Which directive the attribute refers to when there are multiple directives in the same tag in AngularJS

I have a question about directives.

When I define two or more directives and put them on the same element, how can I tell which directive the attribute belongs to? Will it belong to all directives?

Something like that:

<dir1  dir2  attr1="attrvalue"></dir1>

      

Can I access attr1 in both dir1 and dir2?

thank

+3


source to share


1 answer


If you are asking if you can access the attribute in the link function of both directives, the answer is yes.

At most one of these two directives can have a selection scope. This directive will also be able to access the attribute with its scoped scope. For example, you could:



.directive("dir2", function () {
    return function (scope, elem, attr) {
        attr.attr1 == "attrvalue";
    };
})
.directive("attr1", function () {
    return {
        scope: {attr1: "@"}
    }
});

      

+1


source







All Articles