AngularJS $ location.path () does not reload data in view mode

In my angular project, when changing the path using $location.path('/foobar') , the destination view is displayed, but the data does not reload (as a rule, the list is not updated after saving the item and returning to the list).

I tried adding $route.reload() or $scope.apply() , but nothing changed.

I do not know what is wrong or not to make this work.

UPDATE

  • $location.url() doesn't work either
  • I am using angular 1.2.26

UPDATE 2 - ANSWER

Well, after a lot of comments and answers, I think it's time to finish this.
I did not think that would be such a difficult question.

So, my conclusion, giving everything you said:

  • By providing a simple @yvesmancera example, the default behavior of the controller is to reboot
  • In a complex controller with a factory resource and some REST calls, any save or update action should also manually update the list link or start a full list reload

You all gave me good advice, so thanks.

+8
javascript angularjs
source share
6 answers

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 should look like this:

 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!

+2
source share

Use $window.location.href. to reload the page. I just check the $ location document :

Page reload

The $ location service allows you to change only the URL; it does not allow reloading the page. When you need to change the URL and reload the page or go to another page, use the lower-level API, $ window.location.href.

Example:

 $window.location.href = "/your/path/here"; 
+7
source share

I had the same problem only yesterday, if you try to go to the same path you are already in, angular will not try to reload the view and controller. For me, this fixed a "/" at the end of each route in $ routeProvider, for example:

 $routeProvider .when('/', { templateUrl: 'views/home.html', controller: 'HomeCtrl' }) .when('/About/', { templateUrl: 'views/about.html', controller: 'AboutCtrl' }) .when('/Contact/', { templateUrl: 'views/contact.html', controller: 'ContactCtrl' }) 

Edit

Here is a working plunkr with angular 1.2.26

http://plnkr.co/edit/jkGKKCp0djN6Jvy2fIRd?p=preview

+1
source share

You can switch https://github.com/angular-ui/ui-router , it has a $ state.reload () method that can reinitialize the entire controller.

If you do not want to switch ther, the problem is that the controller is still alive, but you can implement it after saving

 $rootScope.$broadcast('data:updated', $scope.data); 

then wrap the method of loading data into the controller to work, and then you can click the new data into the existing list / or do ajax reload

 $rootScope.$on('data:updated',function(listener,data) { $scope.data.push(data); }); $rootScope.$on('data:updated',function() { callAjax.then(function(data) { $scope.data = data; } }); 

https://docs.angularjs.org/api/ng/type/ $ rootScope.Scope # $ on

+1
source share

Try $scope.dataModel.$save(); $location.url('/foobar'); $scope.dataModel.$save(); $location.url('/foobar');
Another reason may solve the problem: when redirecting to / foobar, the foobar controller must have an AJAX call to your server to load new data. And you should use angular factory to make AJAX calls.
If it still does not work, you can provide additional information about the version of angular you are using, as well as about your underlying infrastructure and database.

0
source share
 $location.path("/login"); $timeout(() => $scope.$apply(), 1000); 

works for me

0
source share

All Articles