Peter Boomsma
Peter Boomsma

Reputation: 9846

Watch for changes inside a scope

I have a scope called $scope.activites that stores the response data from a service,

activitiesService.loadActivities().then(function(response) {
  $scope.activities = response.data;
  console.log ($scope.activities)
})

I would like to add a class to a icon in my template when the $scope.activities changes.

%i.fa.fa-bell{"ui-sref" => "home.activities"

And then remove the class when the new activities have been viewed by the user.

I'm thinking I need to use watch and look for changes on the activities scope and then perform an action. I tried adding this code to the activities controller,

$scope.$watch('activities', function(newValue, oldValue) {
  console.log ('new activity')
})

But this just runs everytime I load the activities controller, not just when the scope has changed.

Upvotes: 2

Views: 56

Answers (1)

Iamisti
Iamisti

Reputation: 1710

Because you want to somehow persist a state in your scope (which created by your controller), you can't do it like that. You'll need a service which will store the current state of your 'activities' array/object. Then check the object equality when a new activity loaded to check if the 'state' of that object/array has changed or not.

Upvotes: 1

Related Questions