Shay Friedman
Shay Friedman

Reputation: 4868

Watching a specific member in objects of an array in angularjs

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!

Upvotes: 0

Views: 56

Answers (1)

JB Nizet
JB Nizet

Reputation: 691735

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()', ...

Upvotes: 1

Related Questions