【问题标题】:Checking for current route in directive检查指令中的当前路线
【发布时间】:2014-07-30 14:06:38
【问题描述】:

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

第一次加载后,我可以使用

检测变化
$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

【问题讨论】:

标签: javascript angularjs angularjs-directive


【解决方案1】:

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

PLUNKER

例如

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

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

【讨论】:

    【解决方案2】:

    您需要在控制器中注入 $route 依赖项,然后使用 $route.current 获取当前路由

    Plunker Example

    控制器代码sn-p:

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

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2018-07-05
      • 2017-01-11
      • 2011-04-02
      相关资源
      最近更新 更多