在angularjs中查看数组对象中的特定成员
我有一个对象数组,类似于:在angularjs中查看数组对象中的特定成员,angularjs,Angularjs,我有一个对象数组,类似于: $scope.arr = [{firstName: 'foo', lastName: 'bar'}, {firstName: 'john', lastName: 'doe'}] 我想使用$scope.$watch只查看数组项中的firstName成员。类似于$scope.$watch('arr[*].firstName',…)。可能吗 谢谢 您只需在您的范围内具有以下功能: $scope.getFirstNames = function() { return
$scope.arr = [{firstName: 'foo', lastName: 'bar'}, {firstName: 'john', lastName: 'doe'}]
我想使用$scope.$watch
只查看数组项中的firstName成员。类似于$scope.$watch('arr[*].firstName',…)
。可能吗
谢谢 您只需在您的范围内具有以下功能:
$scope.getFirstNames = function() {
return $scope.arr.map(function(element) {
return element.firstName;
});
}
然后使用
$scope.$watch('getFirstNames()', ...
我怀疑这实际上会触发watcher every
$digest
循环,因为.map
每次都会生成一个新的数组引用。如果$watch(objectEquality
)的第三个参数设置为true,则不会