javascript - How can I replace an object in array that is displayed using ng-repeat? -
i have array of items displayed in table using ng-repeat. when click on item, item pulled server , table should updated updated item.
function updated item when clicking on item in table:
$scope.getupdateditem = function(item){ itemservice.getitem(item).then( function(updateditem){ item = updateditem; }, function(error){ //handle error } ); };
i'm displaying items using:
<tr ng-repeat="item in myitems">
the problem: item in table never updated.
what's best way update item in ng-repeat? can use "track $index" in ng-repeat this? or have iterate on myitems find item want replace?
update:
a possible solution instead of using
item = updateditem,
to use:
var index = $scope.myitems.indexof(item); $scope.myitems[index] = updateitem;
however, feel there should "cleaner" way of doing this.
there isn't cleaner way (then update). noticed, when change item
in callback function change local reference, , not original item in array.
you can improve bit using $index
ng-repeat
, instead of calculating yourself:
<div ng-click="getupdateditem(item, $index)"> </div>
and in controller:
$scope.getupdateditem = function(item, index){ itemservice.getitem(item).then( function(updateditem){ $scope.myitems[index] = updateitem; }, function(error){ //handle error } ); };
you can use angular.copy
instead it's less efficient:
function(updateditem){ angular.copy(updateitem, item); },
Comments
Post a Comment