0

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?
  });
 }
9
  • Is this a single page app? Do you have some higher level service that can hold that information when you change page? How is the app structured between the two pages? If it's not a single page app you may be able to store some info in localstorage which you read when the user navigates tot he other page? If it is single page store the data in the higher level service. Hell you could store an ID in the querystring and use that to get data on the details page. Commented Mar 16, 2016 at 8:24
  • its a spa, yes! Even if i store the data in a local session for example. I am asking how can a now access this alement and update it with angular? Is there a specific way of doing that? Commented Mar 16, 2016 at 8:26
  • Define access this element? If your using data-binding set your model and it will update the view? Do you mean in the repeat or in the details page? Either way when you navigate away from the 'list' screen to the details screen the state of the last screen is lost, its controller cleaned up and you start again on the new screen. Unless your doing something special where you maintain states and don't actually navigate away from the previous page which is why more info is needed on the architecture and communication between the two pages. Commented Mar 16, 2016 at 8:38
  • yes when one navigates away from one page to the other the data is lost but thats ok and not of my concern. can you give me an example based on the code above how to accomplish databinding? Commented Mar 16, 2016 at 8:41
  • Hopefully this demo will help explain jsfiddle.net/3m45vug7 Its the core of angular, there's shedloads out their about it. Commented Mar 16, 2016 at 8:56

2 Answers 2

1

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);
  });
 }
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

that dont seem to be right because i would be loading the entire list only to update one single item in the list?
@jhondano I have added new answer, please check that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.