簡體   English   中英

AngularJS將參數傳遞給指令的模板

[英]AngularJS Passing parameter to directive's template

我有這個基本設置

的HTML

<linear-chart chartData="myArray" height="666"> </linear-chart>

JS ...

 ... app.directive('linearChart', function($window){ return{ restrict:'EA', template:"<svg width='850' height={{myHeight}} ></svg>", ******-> if I hard code height with number all great but when trying to pass as arg it goes kapput with/without the quotes link: function(scope, elem, attrs){ var salesDataToPlot=scope[attrs.chartData]; scope.myHeight = attrs.height; console.log('h passed=', scope.myHeight); *****-> prints out the proper value 666 ... 

嘗試將高度作為參數傳遞時,出現以下錯誤:屬性高度:預期長度“ {{myHeight}}”。

如此純真的東西,但我找不到類似的情況/答案,或者我是否以錯誤的方式將其值傳遞給模板???

更新

感謝bhantol和Kristiyan,盡管Chrome中的屬性仍在指令中設置,但角度代碼仍未轉換范圍。myHeight,我通過Chrome調試器,經過幾次試驗后,我找到了解決方法,它就在這里

 ... template: function(scope,elem) { var myTemp = '<svg width="850" height="' + elem.height + '"></svg>'; return myTemp; }, ... 

您需要將雙引號更改為單引號,例如:

template:"<svg  width='850' height={{myHeight}} ></svg>",

至:

template:'<svg  width='850' height={{myHeight}} ></svg>',

這樣,Angular將使用myHeight持有的任何值,而不是將myHeight作為字符串。 更多信息可以在這里的AngularJS文檔中找到 查看模板擴展指令指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM