检查指令中的当前路线

时间:2014-07-30 14:06:39

标签: javascript angularjs angularjs-directive

首次加载网页时,我的指令控制器需要知道当前选择的路线是什么。

首次加载后,我可以使用

检测更改
$scope.$on('$routeChangeSuccess', function(){});

修改

Answers below are valid but it didn't work for me because:
 - I am new to AngularJS technology i didn't realize that, on our project, we are using custom route provider
 - You need to inject 'ngRoute' in your module before you can use $route

2 个答案:

答案 0 :(得分:0)

您可以使用$routeChangeSuccess回调签名 function(event, currentRoute, previousRoute) {}

<强> PLUNKER

e.g。

$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
  console.log(current.$$route); // displays current $route object
});

当前文档没有显示回调签名(IDK为什么),但您可以在他们的github上看到它们, see this line

答案 1 :(得分:0)

您需要在控制器中注入$ route依赖项,然后使用$ route.current

获取当前路由

Plunker Example

控制器代码段:

.controller('MainController', function($scope, $route, $routeParams, $location) {
   //Here setting scope with $route object
   $scope.$route = $route;
   $scope.$location = $location;
   $scope.$routeParams = $routeParams;
})