2

I currently have data that looks like the following:

$scope.boardLists = [{
cards: [{a:1, b:2}, {a:2, b:2}]
},

{
cards: [{a:3, b:3}, {a:4, b:4}]
},

{
cards: [{a:5, b:5}, {a:6, b:6}]
}];

How can I watch for changes in the cards array?

I have tried the following so far:

$scope.$watchCollection('boardLists', function(newVal, oldVal){
console.log(newVal);
console.log(oldVal);
});

$scope.$watch('boardLists', function(newVal, oldVal){
console.log(newVal);
console.log(oldVal);
}, true);
1
  • 1
    option 2 will work Commented Mar 17, 2016 at 23:09

2 Answers 2

2

Scope $watch Depths

$scope.$watch('boardLists', function(newVal, oldVal){
  console.log(newVal);
  console.log(oldVal);
}, true);

Use $scope.$watch('item',fn,true); for a deep watch.

For information, see AngularJS Developer Guide - $scope Watch Depths.

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

Comments

0

$watch is a bit expensive, you can try to use $apply

$scope.$apply(function() {
  // do something with the scope
  $scope.boardList.push({cards:...});
});

$apply will automatically rerun your templates

2 Comments

I wonder you gave you +1, this "answer" is so unrelated, so random. Funny though.
this is charme, bro :)

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.