Luntegg Luntegg - 4 months ago 42x
AngularJS Question

anular: $watch object in array of objects

For example, I have array of objects.

$scope.items = [
{id: 1, name: 'one'},
{id: 2, name: 'two'},
{id: 2, name: 'three'}

And have in template something like

<div ng-repeat="item in items">
<input type="text" ng-model="" />

And I want add
to this like (for track changes in inputs)

$scope.$watch('item', function(newVal){}, true);

What I need to do, if I want have in
item like
{id: 1, name: 'one'}

Not array of objects! Only one changed object in

I can't create variables for each object of array in controller.

I tried something like

for (var i = 0; i < $scope.items.length; i++) {
$scope.$watch('items[' + i = ']', function(newVal) {
}, true);

but that wrong.


You can try something like

for (var i = 0; i < $scope.items.length; i++) {
            return $scope.items[k]
        }, function(newVal) {
        }, true);

Note: I have not tested the above code;