Reinitialize an Angular.js controller

只谈情不闲聊 提交于 2019-11-29 03:07:05

Just after asking, I finally found one way to solve this using $route.reload().

myapp.Controller('SampleController', function($location, $route) {

  $scope.navTo = function(url) {
    if ($location.path() === url) {
      $route.reload();
    } else {
      $location.path(url);
    }
  }

});

I'm still thinking, that there must be some more elegant solution, but this definitely works for me.

If you are using angular-ui-router then you should do it in this way:

myapp.Controller('SampleController', function($state) {

    $scope.navigate = function (stateName) {
        if ($state.is(stateName)) {
            $state.reload();
        } else {
            $state.go(stateName);
        }
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!