Add Directive Attribute to Directive Element

大城市里の小女人 提交于 2019-12-12 03:33:13

问题


I have a directive element:

return {
        restrict: 'E',
        replace: true,
        transclude: true,
        template:   '<ul>' +
                        '<li {{myNewAttrDirective}}>Home</li>' +
                        '<li {{myNewAttrDirective}}>Products</li>' +
                        '<li {{myNewAttrDirective}}>Cart</li>' +
                        '<li {{myNewAttrDirective}}>Contact Us</li>' +
                    '</ul>',
        scope: {
              _showMore : '=showMore'
        },
        link: function(scope, element, attrs) {

            if (scope._showMore === true) {
               scope.myNewAttrDirective = 'myAnimationDirective'
            }

        }
    };

HTML:

<my-directive show-more="true"></my-directive>

So essentially, if show-more is equal to true, then replace {{myNewAttrDirective}} with an attribute directive i have i.e my-attr-directive.

I have tried with the above if statement, but as expect, in the html, i am left with <li {{myNewAttrDirective}}>Home</li>


UPDATE:

Within my if statement, i can add an attribute via:

var item = element.find("li");
item.attr("myAnimationDirective", "");

Which is rendered in the HTML, but the click events do not respond. (They do work if i added them to my original directive however).


回答1:


Solved adding

$compile(element)(scope);


来源:https://stackoverflow.com/questions/35744655/add-directive-attribute-to-directive-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!