Watch a child property / array with AngularJS

Turns out you cant. You’ll find a lot of people jumping to $scope.$watchCollection, which triggers on inserts/removes/sorts, but not updates.

You can watch all changes on an array simply by passing it $scope.$watch(‘myarray’), but that triggers on any change and doesn’t target a particular array item.

The answer is really to generate a child controller with a child scope.

<div ng-repeat="item in items" ng-controller="ChildController"></div>

<script>
app.controller('ChildController', function($scope) {
$scope.watch('itemProperty', function() {console.log("Child property changed")
});
});
</script>

One thought on “Watch a child property / array with AngularJS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s