2015-04-01 96 views
2

我有一个问题,因为我还不了解AngularJS指令。我在我的HTML像这样一个指令,在AngularJS指令模板中访问attrsUrl

<div my-directive="Hello World"></div> 

在我的指令JS文件:

.directive('myDirective', [function () { 
     return { 
      restrict: 'A', 
      priority: 100, 
      templateUrl: 'views/templates/my-directive.html', 
      link: function (scope, element, attrs) { 
       // I wish to pass attrs.myDirective to the templateUrl 
      } 
     }; 
    }]); 

,并在我的templateUrl的意见/模板/我-directive.html“ :

<h1> {{ attrs.myDirective }} </h1> 

显然,这不工作,我的指令值传递给链接功能,但不给templateUrl,H我可以输出我的templateUrl中的值吗?愚蠢的问题,我知道,但我不知道实现这个最好的方法是什么?

回答

1

您可以简单地将此值分配给范围变量。

scope.myDirective=attrs.myDirective 

app.directive('myDirective', [function () { 
     return { 
      restrict: 'A', 
      priority: 100, 
      templateUrl: 'my-directive.html', 
      link: function (scope, element, attrs) { 
       scope.myDirective=attrs.myDirective 
       // I wish to pass attrs.myDirective to the templateUrl 
      } 
     }; 
    }]); 

Plunker

+0

本来我这个和它的工作,但是我不能肯定,如果我是使用编译功能,这通过ATTR到templateUrl – 2015-04-01 07:12:33

+0

没有必要编译这里,直到你会有动态tempalte :) – squiroid 2015-04-01 07:17:58