Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which directive does the attribute belong 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 in the same element, how can I tell which directive an attribute belongs to? Will it belong to all of the directives?

Something like this:

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

Can I access attr1 in both dir1 and dir2?

Thanks

like image 530
Kuan Avatar asked Dec 17 '25 16:12

Kuan


1 Answers

If you are asking whether 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 an isolate scope. That directive would also be able to access the attribute with its isolate scope. So for example, you could have:

.directive("dir2", function () {
    return function (scope, elem, attr) {
        attr.attr1 == "attrvalue";
    };
})
.directive("attr1", function () {
    return {
        scope: {attr1: "@"}
    }
});
like image 130
Explosion Pills Avatar answered Dec 20 '25 10:12

Explosion Pills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!