1

我知道 Angular 没有包罗万象的控制器。例如:

 $routeProvider.otherwise({
 controller: 'CatchAllCtrl'
 });

但是,如果 $routeProvider.otherwise 被触发,有没有办法调用一些代码,所以我至少可以知道 else 被调用以及路由是什么?

4

3 回答 3

2

您可以重定向到 catchcall 路由,该路由可以执行您的“否则”逻辑,然后手动重定向到应用程序中的实际视图。

$routeProvider
  .when('/', {
    templateUrl: '/partials/index.html', 
    controller: 'homeController'
  })
  .when('/otherwise', {
    controller:  'otherwiseController'
  })
  .otherwise({redirectTo: '/otherwise' });

在你的 elseController 中,你可以注入 $location 然后设置$location.path("/")重定向到你想去的任何地方。

您可能需要为 /otherwise 路由指定一个 templateUrl(我不确定)。

于 2013-10-21T22:31:51.887 回答
1

您可以监听路由更改并在回调中执行所需的操作。

  $rootScope.$on('$routeChangeSuccess', function() {
    var path = $location.path();
     // this shoudl fire for all routes
  }):
于 2013-10-21T22:27:04.017 回答
0

有点像 Angular 新手,所以把所有东西都加盐 :)

你不能简单地将你想要执行的代码放在“CatchAllCtrl”里面的“otherwise”上吗?

或者,您可以在到达“否则”时更新控制变量并使用$scope.watch来捕捉它的变化 如何在 AngularJS 中使用 $scope.$watch 和 $scope.$apply?

于 2013-10-21T22:29:04.467 回答