-1

I'm learning Angular, I have a simple directive that render a select. On the ng-change of that select I'm calling a method in the controller of my directive:

$scope.changeSelect = function(selectedValue) {
  exampleService.setNewValue(selectedValue)
}

that service that I have created, updates an example value I have created.

Besides that, I have another controller and a view that renders the directive. In that view.

How can I tell the controller that the directive has changed, and do something with the data?

Pablo
  • 9,424
  • 17
  • 55
  • 78
  • 1
    Seriously, this gets asked about 10 times a day.... do a google search, there are so many examples. I've wrote about 3 answers to this so far.... – Callum Linington Aug 03 '16 at 15:51
  • 1
    This maybe true, but people ask things differently and considering he is new to angular he may not know what to look for. – Jon Aug 03 '16 at 15:53

2 Answers2

0

You would want to use $scope.$watch to listen for changes to the variable that you are passing. Check out https://docs.angularjs.org/api/ng/type/$rootScope.Scope to better understand how watch works

$scope.$watch('yourValue', function (oldValue, newValue) {
    //do something
});
Jon
  • 2,456
  • 21
  • 28
0

Use ng-model at your select input, then this code:

$scope.$watch('modelvalue', function(newValue, oldValue) {
     exampleService.setNewValue(selectedValue)
}, true);
Jorge Alberto
  • 167
  • 1
  • 6
  • yes, this is how I update in the service, my problem is the controller to know of this change – Pablo Aug 03 '16 at 16:21