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 Answers2

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.

Gene Parcellano
  • 5,799
  • 5
  • 36
  • 43
  • 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. – HJ05 Dec 15 '15 at 18:43
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.

Community
  • 1
  • 1
UtsavShah
  • 857
  • 1
  • 9
  • 20
  • 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. – Michael Krukar Dec 17 '15 at 17:22