Reputation: 3384
I have a select input with a controller that- when a value is selected- wants to pass that value to a directive that will display it. (In this case, draw a shape on a leaflet map).
The issue is I want the value to be "fire and forget"- so they select the shape, the select box resets to empty again immediately, and the directive in charge of drawing receive the shape and draw it.
My initial thought is to have a service that links the two: so when the shape is selected, the select controller updates the service and sets the drawThisShape
variable, which is referenced on the directives $scope and being $watch
ed: when it changes, the directive draws the shape. Happy days.
This seems like a lot of effort to go to just to send a variable from one place to another though. And the service gets left in a state of constantly having the "previous" shape on it's model despite it not being needed anymore.
Perhaps I'm being too fussy but I feel like there must be a better way to achieve this?
E: For clarification, the controller is the controller for the select input, the directive I want to pass it to is a directive for a leaflet map instance, they are not linked in any way
Upvotes: 0
Views: 59
Reputation: 3820
Well, I have used the technique you've described in the past. Another way of achieving what you need is to broadcast an event on the $rootScope (or any other parent scope of drawShape directive) from the select input that the drawShape directive is listening for.
See scope event propogation and broadcasting
Upvotes: 1