2013-05-01 48 views
4

角JS - 有沒有辦法該指令的屬性傳遞給模板

<rn-text-edit rn-scope="profile.first_name"></rn-text-edit> 

上的JS

app.directive("rnTextEdit", function() { 
    return { 
     restrict: 'E', 
     replace: true, 
     template:'<span>{{'+rn-scope+'}}</span>' 
    } 
}); 

我知道我可以代替DOM,並通過訪問屬性鏈接。我想知道是否有方法將指令的屬性傳遞給模板。

+0

的範圍

fiddle

更多的信息和'@'指令可以使用分離的範圍? – 2013-05-01 21:24:57

回答

6

如果你只是顯示值:

<rn-text-edit rn-scope="{{profile.first_name}}"></rn-text-edit> 

-

app.directive("rnTextEdit", function() { 
    return { 
     restrict: 'E', 
     replace: true, 
     scope: { 
      rnScope: '@' 
     }, 
     template: '<span>{{rnScope}}</span>' 
    } 
}); 

如果該指令需要修改的值,可以使用'='並跳過雙花括號。在Angular Directives page

+1

我認爲'@'會更適合這種情況,因爲指令似乎不需要改變值。 – 2013-05-01 21:30:15

+0

好點,更新了答案。謝謝馬克 – 2013-05-01 21:39:05

+0

謝謝。這就是我正在尋找的。 – simmu 2013-05-02 13:30:49

相關問題