我如何获得$ routeParams自定义指令内 [英] How do I get $routeParams inside a custom directive

查看:129
本文介绍了我如何获得$ routeParams自定义指令内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个导航栏的指令,我想用$ routeParams确定要设置为激活哪个环节。我可以访问$ routeParams在控制器没有问题:

I am creating a navBar directive and I would like to use $routeParams to determine which link to set to active. I can access $routeParams in controllers with no problem:

app.controller('MovieDetailsController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
        $scope.movie = {};
        console.log($routeParams);
    }]);

不过,我无法从$在我的指令routeParams得到任何数据:

However, I am unable to get any data from $routeParams in my directive:

app.directive('navBar', ['$routeParams', function ($routeParams) {
        console.log($routeParams);
    }]);

我在做什么错了?

What am I doing wrong?

推荐答案

好像当指令初始化$ routeParams不可用等。如果你使用的指令如下面作为普拉克它应该工作。

Seems like when the directive is initialized $routeParams is not available. If you use the directive like below as in the plunk it should work.

app.directive('myDir', ['$routeParams', function ($routeParams) {
    return {
      restrict: 'A',
      link: function(scope, element, attrs) {
        element.bind('click', function() {
          alert($routeParams.number);
        });
      }
    }
  }]);

http://plnkr.co/edit/joHLLqtsUK3xCAVemmeg?p=$p$ PVIEW

这篇关于我如何获得$ routeParams自定义指令内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆