AngularJS watch array of objects for data change AngularJS watch array of objects for data change angularjs angularjs

AngularJS watch array of objects for data change


$watch only evaluate string or function parameter in its first argument. Change your $watch like this :

$scope.$watch('cart.name + cart.id + cart.amount', $scope.updateCart());

OR

$scope.$watch('cart', $scope.updateCart, true);

See reference API


It is a common mistake to use a variable as the first parameter to the watch expression.

For $scope.cart, you should use the string cart:

$scope.$watch('cart', function () {...}, true)

AngularJs has a new watch method, $watchCollection. It is basically the same as $watch with the object equality option set to true, to watch for a change in a property or an array item.

$scope.$watchCollection('cart', function () {...});

Documentation: $watchCollection.


try to change

$scope.$watch($scope.cart, $scope.updateCart(), true);

to

$scope.$watch('cart', $scope.updateCart(), true);

$watch only evaluate string or function parameter in its first argument