2

In Angular.js, I have this in my directive:

    scope.$watchCollection(function(){
          return StatusTrackerService.eventCollection
      }, function(){
          console.log("ssssssssssss");
          console.log(StatusTrackerService);
          console.log("ssssssssssss");
      });

In my service, I am feeding it StatusTrackerService.eventCollection and add a property to eventCollection:

    function runEventCheck(account, eventId) {
        url = urlBuilder(account, eventId);

        eventCollection[referenceVipLabel.vipLabel] = new NewStatusEvent(account, eventId, url);
    }
    return { runEventCheck: runEventCheck,
             eventCollection: eventCollection,
             referenceVipLabel: referenceVipLabel
    };
});

When I add a property to eventCollection, $watch does not detect a change. It seems to me adding a property to object eventCollection would change the object. Why isn't $watch detecting this change? I don't really want to use $watchCollection because then it detects any changes to StatusTrackerService.

2
  • eventCollection is StatusTrackerService.eventCollection right? Commented Aug 13, 2014 at 17:20
  • yes, eventCollection is StatusTrackerService.eventCollection Commented Aug 13, 2014 at 17:27

1 Answer 1

3

You need to do a deepwatch on the property. You can do it by setting the third argument for object equality on the watch.

 scope.$watch(function(){
      return StatusTrackerService.eventCollection; 
  }, function(v){
       console.log(v);
   }, true); //<-- Here

Plnkr

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

Comments

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.