AngularJS accessing DOM elements inside directive template

后端 未结 3 573
遥遥无期
遥遥无期 2021-01-30 10:19

Is there a more \"angular\" way of selecting DOM elements inside a directive template? For example, say you have this directive:

app.directive(\"myDirective\", f         


        
3条回答
  •  有刺的猬
    2021-01-30 10:54

    This answer comes a little bit late, but I just was in a similar need.

    Observing the comments written by @ganaraj in the question, One use case I was in the need of is, passing a classname via a directive attribute to be added to a ng-repeat li tag in the template.

    For example, use the directive like this:

    
    

    And get the following html:

    • Item 1
    • Item 2

    The solution found here applied with templateUrl, would be:

    app.directive("myDirective", function() {
        return {
            template: function(element, attrs){
                return '
    '; }, link: function(scope, element, attrs) { var list = element.find("ul"); } } });

    Just tried it successfully with AngularJS 1.4.9.

    Hope it helps.

提交回复
热议问题