2015-02-08 76 views

回答

2

attr是在範圍訪問,這樣你就可以訪問scope.attr在您的控制器或鏈接階段,或{{attr}}在模板中。一個簡單的解決方案是將您的模板更改爲

mymodule.directive('eicon', function(){ 
    return { 
     restrict: 'E', 
     scope: { 
      attr: '=' 
     }, 
     template: "test {{attr.name}}", 
     link: function (scope, element, attrs) { 
      console.log(scope.attr); 
     }, 
     controller: function (scope) { 
      console.log(scope.attr); 
     } 
    } 
}); 
3

既然你宣佈獨立scope屬性attr你應該能夠訪問scope.attr模板是這樣的:

mymodule.directive('eicon', function(){ 
    return { 
     restrict: 'E', 
     scope: { 
      attr: '=' 
     }, 
     template: "test {{attr.name}}" 
    } 
}); 

演示:http://plnkr.co/edit/YZ0aPqhkMlIIwmrkKK2v?p=preview

+0

如果我需要訪問代碼呢?該模板將取決於該值 – brazorf 2015-02-08 13:08:19

+0

你是什麼意思「訪問代碼」?你是否在談論可以用'scope.attr'對象操作的鏈接函數? – dfsq 2015-02-08 13:10:25

+0

@dfsq你可以檢查我的答案樣本 – Rebornix 2015-02-08 13:16:17