AngularJS: How to target specific directive with multiple instances
angularjs
Solution
You should do this check in your event handler:
myApp.directive('myDirective', function() {
return {
restrict: 'A',
controller: function(scope, el, attrs) {
scope.$on('myEvent', function(ev,args) {
//do the check - you could provide a function, a value or something else
if(el.hasClass(args.class)){
el.css({left: '+=100'});
}
});
},
};
});
Then add the parameters in the $broadcast
$rootScope.$broadcast('myEvent',{class:'one'})
Problem
I have multiple instances of a directive and I would like to target a specific instance only. For example in the code below, how can I make sure that the div with `class="one"` is the only one that gets triggered by the event `$rootScope.$broadcast('myEvent')`. JS: ``` myApp.directive('myDirective', function() { return { restrict: 'A', controller: function(scope, el, attrs) { scope.$on('myEvent', function() { el.css({left: '+=100'}); }); }, }; }); ``` HTML: ``` <div my-directive class="one"></div> <div my-directive class="two"></div> ```