1

I have a directive project. Multiple projects are set on a page. Based on a data attribute it sets a scope variable to true or false. I want to be able to set all scope variables to false.

I can provide code if necessary but essentially my question is how can I click on a button to affect the scope variable in every single instance of a directive on a page?

2 Answers 2

1

You could use a $rootScope.$broadcastand a $scope.$on, that acts like an event dispatching and listening system. Look at this for more details on how to implement this.

Sign up to request clarification or add additional context in comments.

1 Comment

I ended up going with this solution, since it made more sense in the context of my project. For those wondering I had a directive with multiple instances on the page. A rootscope broadcast notified all of them to react accordingly. Using $watch in a service felt like reinventing the wheel, where as this worked with very little code.
1

Michael, I would create a service where that data would live. On each directive, I would $watch that service and update the variables when it changes.

1 Comment

I think a small change would work a little better, since the OP seems to already have the directive changing value in one controller, change that controller to get/set the value within the service then all other controllers that know about that service will get the update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.