jhon dano
jhon dano

Reputation: 658

angular.js updating object data from ng repeat

i have two pages in my app, one who displays a list of things and the second which displays a single item from that list. I have build a function which updates the status by calling server side api and then returns the modfied item How can i now get my view to be updated with the new item? I now would like to update the status from my list as well on the item detail page? Obvi

I call the function this way:

 ... href="#" ng-click="change_status(i.item_id,2)">In Text</div>

in my controller:

 $scope.change_status = function (id, sts) {
    MySrv.changeStatus(
    { item_id: id, item_sts: sts, user_id: window.UD.user_id      
 })
 .then(function (response) {  
  //I am getting back my object here ->How can i now 
  //update the view for this specific object?
  });
 }

Upvotes: 0

Views: 43

Answers (2)

Shailendra Singh Deol
Shailendra Singh Deol

Reputation: 637

You can also use some short of method to replace only current object. You need to pass current index of that object in change_status function and after succeed, you need to replace that object by using splice function like as -

$scope.change_status = function (id, sts , index) {
    MySrv.changeStatus(
    { item_id: id, item_sts: sts, user_id: window.UD.user_id      
 })
 .then(function (response) {  
     yourObjectList.splice(index,1,response);
  });
 }

Upvotes: 1

Shailendra Singh Deol
Shailendra Singh Deol

Reputation: 637

You need to call that funtion which is providing list when 'change_status' succeed like as -

$scope.change_status = function (id, sts) {
    MySrv.changeStatus(
    { item_id: id, item_sts: sts, user_id: window.UD.user_id      
 })
 .then(function (response) {  
 // here call that function which will get list again like $scope.getList()
  });
 }

Angular will automatically bind data again in ng-repeat

Upvotes: 0

Related Questions