Reinitialize an Angular.js controller

后端 未结 2 1427
天涯浪人
天涯浪人 2020-12-16 12:14

if you have a controller to manipulate $scope variables in Angular.js, is there an idiomatic way to:

  • reset the controller\'s $scope, and
  • restart contr
2条回答
  •  时光说笑
    2020-12-16 12:57

    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.

提交回复
热议问题