0

I have an array of objects, something like:

$scope.arr = [{firstName: 'foo', lastName: 'bar'}, {firstName: 'john', lastName: 'doe'}]

I want to use $scope.$watch to watch only the firstName member in the array items. Something like $scope.$watch('arr[*].firstName', ... ) . Is it possible?

Thanks!

1 Answer 1

1

you could simply have the following function in your scope:

$scope.getFirstNames = function() {
    return $scope.arr.map(function(element) {
        return element.firstName;
    });
}

And then use

$scope.$watch('getFirstNames()', ...
Sign up to request clarification or add additional context in comments.

2 Comments

I suspect that this will actually trigger the watcher every $digest loop since the .map will produce a new array reference every time.
Not if the third argument to $watch (objectEquality) is set to true

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.