AngularJS - Find Element with attribute

前端 未结 3 1693
南方客
南方客 2020-12-23 10:05

I\'m new to AngularJS. I\'ve learned that I can find elements in the DOM using queries like the following:

var e = angular.element(document.querySelector(\'#         


        
3条回答
  •  一个人的身影
    2020-12-23 10:15

    Rather than querying the DOM for elements (which isn't very angular see "Thinking in AngularJS" if I have a jQuery background?) you should perform your DOM manipulation within your directive. The element is available to you in your link function.

    So in your myDirective

    return {
        link: function (scope, element, attr) {
            element.html('Hello world');
        }
    }
    

    If you must perform the query outside of the directive then it would be possible to use querySelectorAll in modern browers

    angular.element(document.querySelectorAll("[my-directive]"));
    

    however you would need to use jquery to support IE8 and backwards

    angular.element($("[my-directive]"));
    

    or write your own method as demonstrated here Get elements by attribute when querySelectorAll is not available without using libraries?

提交回复
热议问题