Sylver Sylver - 3 months ago 13
AngularJS Question

AngularJS $location.path() not reloading data of the destination view

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.

Answer

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!!