2015-04-01 98 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