AngularJS $ location.path()不重新加载目标视图的数据

Syl*_*ver 9 javascript angularjs

在我的角度项目中,当显示更改具有$location.path('/foobar')目标视图的路径但未重新加载数据时(通常在保存项目并返回列表后,列表不会更新).

我试图添加$route.reload()$scope.apply(),但没有任何改变.

我不知道做这项工作有什么不对或缺失.

UPDATE

  • $location.url() 也没有'工作
  • 我正在使用角度1.2.26

更新2 - 答案

好的,经过大量的评论和回答,我认为是时候结束这个了.
我认为这不是一个如此复杂的问题.

所以,我的结论,给你所说的是:

  • 给出@yvesmancera的简单示例,控制器的默认行为是重新加载自身
  • 在具有资源工厂和一些REST调用的复杂控制器中,任何保存或更新操作也应手动更新列表引用,或触发列表的完全重新加载

你们所有人都给了我一些好的建议,谢谢.

Huy*_*yen 8

使用$window.location.href.重新加载页面.我只是查看$ location文件:

页面重新加载导航

$ location服务允许您仅更改URL; 它不允许您重新加载页面.当您需要更改URL并重新加载页面或导航到其他页面时,请使用较低级别的API $ window.location.href.

例:

$window.location.href = "/your/path/here";
Run Code Online (Sandbox Code Playgroud)

  • 这儿存在一个问题.是$ window.location.href仅在路径与当前路径不同时才重新加载页面.在AngularJS中,通常路径不会改变,但只有锚点后的部分(#).因此,如果停留在/#/ some-route-1,则调用$ window.location.href ="/#/ some-route-2"将不会触发页面重新加载. (2认同)

Sud*_*ary 2

伪代码:-

    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. 
    }
    }]);
Run Code Online (Sandbox Code Playgroud)

你的服务应该是这样的,

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.
}
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!!