javascript - Angular Directive to Directive call -
if have directive you're using multiple times on page how can 1 directive communicate another?
i'm trying chain directives in parent child relationship. when directive clicked want filter directive b have children of selected item in directive a. in case there may infinite number of directives , relationships on page.
normally have directive call filter method on each of it's children, , each child calls it's child continue filtering down hierarchy.
but can't figure out if calling methods 1 directive possibe.
thanks
it sounds looking directive controller. can use require:
parameter of directive pull in directive's controller. looks this:
app.directive('foo', function() { return { restrict: 'a', controller: function() { this.qux = function() { console.log("i'm foo!"); }; }, link: function(scope, element, attrs) { } }; }); app.directive('bar', function() { return { restrict: 'a', require: '^foo', link: function(scope, element, attrs, foo) { foo.qux(); } }; });
from angular docs, here symbols can use require , do.
(no prefix) - locate required controller on current element. ? - attempt locate required controller, or return null if not found. ^ - locate required controller searching element's parents. ?^ - attempt locate required controller searching element's parents, or return null if not found.
here's jsbin of example. http://jsbin.com/alikef/1/edit
another option may work need have service each directive sets watch on , can manipulate. example, directive1 may watch property in service , respond changes , setup button can change property. then, directive2 can watch , change service, , respond 1 set up. if need jsbin of also, let me know.
i hope helps!
Comments
Post a Comment