0

Watch is not being called on change in the item. It works on load, and loads the map correctly. However when I change event_type with a drop down, and it never makes it inside the function. I checked with concole.log.

Actual event_type does change, it displays new value on the screen with {{ event_type.type }}

$scope.event_type = { id: 0, type:'All' }
$scope.$watch( 'event_type', function(val) {
    $scope.markers = Events.venues(val.type)
    }, true)
rodling
  • 988
  • 5
  • 18
  • 44

1 Answers1

1

I think it's called deep diving or deep watching. Regardless, it's a 3rd parameter on the $watch you want to set to true.

You also can pass in the new value as a parameter instead of reading from event_type. As seen below.

$scope.$watch( 'event_type', function(newVal) {
    $scope.markers = Events.venues(newVal.type)
}, true);

Here's a plunker: http://plnkr.co/edit/YMmVPKDMeUcK5Pvz2Z0z?p=preview

Let me know if something here doesn't make sense.

Timothy
  • 1,198
  • 3
  • 10
  • 30
  • It's called deep watching. This may help: http://stackoverflow.com/questions/14712089/how-to-deep-watch-an-array-in-angularjs – Timothy Nov 21 '14 at 20:53
  • still nothing, never enters the function second time – rodling Nov 21 '14 at 21:11
  • Did you pass true to the watch? That's the important part here. $scope.$watch('event_type', function() {..}, true); – Timothy Nov 21 '14 at 21:14
  • Nevermind, I see from your new code that you did. It seems like the $watch is actually working properly and it may be a binding issue or something else. Are you able to make a plnkr that shows more of what you're trying to accomplish? – Timothy Nov 21 '14 at 21:16
  • would be fairly complicated, it uses a dropdown to get the value with ng-model and pass it to google map markers. – rodling Nov 21 '14 at 21:27
  • Gotcha. "Actual event_type does change, it displays new value on the screen with {{ event_type.type }}" hints that the $watch does work. You could add a console.log('Value has changed'); to verify, but I don't think it's an issue with the watch at this point. – Timothy Nov 21 '14 at 21:49
  • It logs first time on load, but when it changes .. it doesnt change. So watch never is run again – rodling Dec 04 '14 at 20:40