我想在链接函数中使用指令的名称。我怎么能得到它?

app.directive('myDirective', function() {
  return {
    link: function(scope, element, attrs) {
      // How could I get directive's name here (i.e. 'myDirective')?
    }
  };
});

最佳答案

只需在注入(inject)之外定义它:

var name = 'myDirective';
app.directive(name, function() {
  return {
    link: function(scope, element, attrs) {
      console.log(name); // --> myDirective
    }
  };
});

关于AngularJS:如何在链接函数中获取指令名称?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22585948/

10-11 14:57