The getElementById method returns the element with the ID Attribute with the specified value. This method is most common in HTML DOM and is used almost every time we manipulate, get info from, or element in our document.
The document. getElementbyId( "myId") is faster because its direct call to JavaScript engine. jQuery is a wrapper that normalizes DOM manipulation in a way that works consistently in every major browser.
The angular. element() Function in AngularJS is used to initialize DOM element or HTML string as an jQuery element. If jQuery is available angular. element can be either used as an alias for jQuery function or it can be used as a function to wrap the element or string in Angular's jqlite.
It can work like that:
var myElement = angular.element( document.querySelector( '#some-id' ) );
You wrap the Document.querySelector()
native Javascript call into the angular.element()
call. So you always get the element in a jqLite or jQuery object, depending whether or not jQuery
is available/loaded.
Official documentation for angular.element
:
If jQuery is available,
angular.element
is an alias for thejQuery
function. If jQuery is not available,angular.element
delegates to Angulars built-in subset ofjQuery
, that called "jQuery lite" or jqLite.All element references in
Angular
are always wrapped with jQuery orjqLite
(such as the element argument in a directives compile or link function). They are never rawDOM
references.
In case you do wonder why to use document.querySelector()
, please read this answer.
You should read the angular element docs if you haven't yet, so you can understand what is supported by jqLite and what not -jqlite is a subset of jquery built into angular.
Those selectors won't work with jqLite alone, since selectors by id are not supported.
var target = angular.element('#appBusyIndicator');
var target = angular.element('appBusyIndicator');
So, either :
Edit: Note that jQuery should be loaded before angularJS in order to take precedence over jqLite:
Real jQuery always takes precedence over jqLite, provided it was loaded before DOMContentLoaded event fired.
Edit2: I missed the second part of the question before:
The issue with <input type="number">
, I think it is not an angular issue, it is the intended behaviour of the native html5 number element.
It won't return a non-numeric value even if you try to retrieve it with jquery's .val()
or with the raw .value
attribute.
var target = document.getElementById('appBusyIndicator');
is equal to
var target = $document[0].getElementById('appBusyIndicator');
If someone using gulp, it show an error if we use document.getElementById()
and it suggest to use $document.getElementById()
but it doesn't work.
Use -
$document[0].getElementById('id')
I don't think it's the right way to use angular. If a framework method doesnt exist, don't create it! This means the framework (here angular) doesnt work this way.
With angular you should not manipulate DOM like this (the jquery way), but use angular helper such as
<div ng-show="isLoading" class="loader"></div>
Or create your own directive (your own DOM component) in order to have full control on it.
BTW, you can see here http://caniuse.com/#search=queryselector querySelector is well supported and so can be use safely.
You can access elements using $document ($document need to be injected)
var target = $document('#appBusyIndicator');
var target = $document('appBusyIndicator');
or with angular element, the specified elements can be accessed as:
var targets = angular.element(document).find('div'); //array of all div
var targets = angular.element(document).find('p');
var target = angular.element(document).find('#appBusyIndicator');
You should inject $document in your controller, and use it instead of original document object.
var myElement = angular.element($document[0].querySelector('#MyID'))
If you don't need the jquery style element wrap, $document[0].querySelector('#MyID') will give you the DOM object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With