Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  10] [ 5]  / answers: 1 / hits: 20623  / 9 Years ago, thu, june 25, 2015, 12:00:00

In my angular project, when changing the path with $location.path('/foobar') the destination view is displayed but the data aren't reloaded (typically after saving an item and going back to the list, the list is not updated).



I tried to add $route.reload() or $scope.apply(), but nothing change.



I don't know what's wrong or missing to make this work.



UPDATE




  • $location.url() doesnt' work either

  • I'm using angular 1.2.26



UPDATE 2 - ANSWER



Ok, after a lot of comments and answers, I think it's time to end this.

I didn't think it would have been a so complicated question.



So, my conclusion, giving all you said is :




  • Giving simple example of @yvesmancera, the default behavior of the controller is to reload itself

  • In a complex controller with a resource factory and some REST calls, any save or update action should also manually update the list reference, or trigger a full reload of the list



All of you gave me some good advices, so thank you.


More From » angularjs

 Answers
329

Pseudo Code:-



    app.controller('myController', ['$scope', '$location','$http', 'ItemListService'
function($scope, $location, $http, ItemListService){
$scope.data = function(){
ItemListService.getAllItems(); //get all the items;
};
$scope.saveMethod = function(item){
$scope.data = ItemListService.save(item); //this is the refresh part, return data through save method. Pull the latest data and bind it to the scope.
$location.path('/fooView'); //dont think you even need this if you are entering data in a modal sorta thing, which on the same view.
}
}]);


You service should look like,



app.service('ItemListService', function(){
this.getAllItems = function(){
//get the items from itemList
//return all the items
}

this.save = function(item){
//save the item in itemList
//**return all items again, call getAllItems here too.
}
});


Hope this helps!!


[#66052] Tuesday, June 23, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alysas

Total Points: 616
Total Questions: 111
Total Answers: 124

Location: Slovenia
Member since Wed, Apr 6, 2022
2 Years ago
alysas questions
Sun, Jul 19, 20, 00:00, 4 Years ago
Tue, Jun 23, 20, 00:00, 4 Years ago
Mon, Mar 30, 20, 00:00, 4 Years ago
;