Two directives on the same element can not both have isolated scope, but can they both use the same scope isolated from their parent? And can they both use properties bound to the isolated scope?
For example, if I have two directives on an element
<e-directive a-directive prop="parentProp"/>
And one directive defines an isolated scope with a bound property
App.directive('eDirective', function() {
return {
restrict: 'E',
scope: {
localProp: '=prop'
},
...
};
});
Does the other directive get that scope and can it use the bound property?
App.directive('aDirective', function() {
return {
restrict: 'A',
link: function postLink(scope, element, attrs) {
scope.$watch('localProp', function(newProp, oldProp) {
...
}
},
...
};
});
My initial attempt (pretty much coded as above) failed.
I suggest you make use of communicating between the directives' controllers via the require property of the secondary directive. The first directive (e-directive) holds the isolated scope, while the second helper directive (a-directive) has a reference to the first directive and sets properties via functions defined on the first directive. A small sample would be (see plunker):
and the javascript:
Yes by using
element.isolateScope()
for example (or see fiddle):HTML
JS
console output
Instead of an isolate scope, the directives can create a new child scope, which will be shared by both directives. If you need to modify
parentProp
in a directive, inject and use$parse
:Javascript:
fiddle
If both directives need to create properties on the new child scope, use some kind of naming convention to prevent name clashes. E.g.,
scope.eDirProp1 = ...
andscope.aDirProp1 = ...
.