angular.element vs document.getElementById or jQuery selector with spin (busy) control

后端 未结 10 2300
感动是毒
感动是毒 2020-11-28 02:18

I\'m using the \"Angularised\" version of the Spin control, as documented here: http://blog.xvitcoder.com/adding-a-weel-progress-indicator-to-your-angularjs-application/

相关标签:
10条回答
  • 2020-11-28 02:42

    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.

    0 讨论(0)
  • 2020-11-28 02:44

    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.

    0 讨论(0)
  • 2020-11-28 02:45

    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 :

    • you use jqLite alone, more limited than jquery, but enough in most of the situations.
    • or you include the full jQuery lib in your app, and use it like normal jquery, in the places that you really need jquery.

    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.

    0 讨论(0)
  • var target = document.getElementById('appBusyIndicator');
    

    is equal to

    var target = $document[0].getElementById('appBusyIndicator');
    
    0 讨论(0)
提交回复
热议问题