Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicating with a directive instance in AngularJS

I have a directive with an isolate scope being rendered inside the template of an outer directive.

I want to send a notification message to the child directive to have it cancel a timer. How can I do this idiomatically?

Will scope.$broadcast do this for me even though the child scope is isolate?

like image 504
Ben Aston Avatar asked Jun 28 '26 20:06

Ben Aston


1 Answers

From documentation for scope.$broadcast:

Dispatches an event name downwards to all child scopes (and their children) notifying the registered $rootScope.Scope listeners.

So it will perfectly work in your case. Note, that despite of being isolated the scope of the inner directive is still child scope of the outer. Isolated just means that this scope doesn't inherit prototypically, however its $parent points to the real parent scope. so $broadcast will work in this case of course.

Here is a little example I set up to test it:

Demo: http://plnkr.co/edit/OoUqgV8oRofakXVf2OZv?p=preview

like image 158
dfsq Avatar answered Jun 30 '26 10:06

dfsq



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!