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
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: "@"}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With