简体   繁体   English

AngularJS将参数传递给指令的模板

[英]AngularJS Passing parameter to directive's template

I have this basic set 我有这个基本设置

HTML 的HTML

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

JS ... 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 ... 

when try to pass the height as an argument I get this Error: attribute height: Expected length, "{{myHeight}}". 尝试将高度作为参数传递时,出现以下错误:属性高度:预期长度“ {{myHeight}}”。

Something so innocent but I cannot find a similar situation/answer or am I passing it the value to the template the wrong way??? 如此纯真的东西,但我找不到类似的情况/答案,或者我是否以错误的方式将其值传递给模板???

UPDATE 更新

Thanks bhantol and Kristiyan although the properties in Chrome were being set still in the directive the angular code was still not translating the scope.myHeight, I stepped thru the Chrome debugger and after a few trials I found out a work around and it is here 感谢bhantol和Kristiyan,尽管Chrome中的属性仍在指令中设置,但角度代码仍未转换范围。myHeight,我通过Chrome调试器,经过几次试验后,我找到了解决方法,它就在这里

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

You need to change your double quotes to single quotes on the following like from: 您需要将双引号更改为单引号,例如:

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

to: 至:

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

That way Angular takes the whatever value myHeight holds rather than taking myHeight as a string. 这样,Angular将使用myHeight持有的任何值,而不是将myHeight作为字符串。 More information can be found in the AngularJS Documentation here ; 更多信息可以在这里的AngularJS文档中找到 check the Template-expanding directive guide 查看模板扩展指令指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM