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

后端 未结 10 2299
感动是毒
感动是毒 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:23

    This worked for me well.

    angular.forEach(element.find('div'), function(node)
    {
      if(node.id == 'someid'){
        //do something
      }
      if(node.className == 'someclass'){
        //do something
      }
    });
    
    0 讨论(0)
  • 2020-11-28 02:24

    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 the jQuery function. If jQuery is not available, angular.element delegates to Angulars built-in subset of jQuery, that called "jQuery lite" or jqLite.

    All element references in Angular are always wrapped with jQuery or jqLite (such as the element argument in a directives compile or link function). They are never raw DOM references.

    In case you do wonder why to use document.querySelector(), please read this answer.

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

    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');
    
    0 讨论(0)
  • 2020-11-28 02:33

    Maybe I am too late here but this will work :

    var target = angular.element(appBusyIndicator);
    

    Notice, there is no appBusyIndicator, it is plain ID value.

    What is happening behind the scenes: (assuming it's applied on a div) (taken from angular.js line no : 2769 onwards...)

    /////////////////////////////////////////////
    function JQLite(element) {     //element = div#appBusyIndicator
      if (element instanceof JQLite) {
        return element;
      }
    
      var argIsString;
    
      if (isString(element)) {
        element = trim(element);
        argIsString = true;
      }
      if (!(this instanceof JQLite)) {
        if (argIsString && element.charAt(0) != '<') {
          throw jqLiteMinErr('nosel', 'Looking up elements via selectors is not supported by jqLite! See: http://docs.angularjs.org/api/angular.element');
        }
        return new JQLite(element);
      }
    

    By default if there is no jQuery on the page, jqLite will be used. The argument is internally understood as an id and corresponding jQuery object is returned.

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

    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')
    
    0 讨论(0)
  • 2020-11-28 02:35

    Improvement to kaiser's answer:

    var myEl = $document.find('#some-id');
    

    Don't forget to inject $document into your directive

    0 讨论(0)
提交回复
热议问题