[英]How to write a function that returns a script as a string in javascript
I'm using angular and I want to append a script to the template of my directive. 我正在使用angular,并且想将脚本附加到指令模板中。 I want to accomplish this by writing a function that accepts the parameters the script needs and then returning it as a string.
我想通过编写一个接受脚本所需参数的函数,然后将其作为字符串返回来实现此目的。 I would like to do it this way so that I dont have to write long javascript files as strings.
我想这样做,这样我就不必将长的javascript文件编写为字符串。
I have the following directive: 我有以下指令:
function barchart() {
return {
restrict: 'EA',
replace: true,
template: `
<div id="canvas" class ="panel-body container-fluid">
<div class ="ui centered grid">
<div>
<div id="title"></div>
<div id="chart"></div>
</div>
</div>
</div>
`,
scope: {
revenues: "=revenues"
},
link: function ($scope, $element, $attrs) {
$scope.$watch('revenues', function (rev) {
if (typeof rev != 'undefined') {
$element.append(`<script> (render(${rev}))(); </script>`);
}
});
}
}
}
I want something around these lines, but it doesn't work 我想在这方面做些事情,但这没用
function render(params){
return (function(){
$('#chart').append('<p> params </p>');
})();
}
rev is not defined
用${
, }
环绕render()
调用
$element.append(`<script>${render(rev)}</script>`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.