简体   繁体   English

如何编写在JavaScript中以字符串形式返回脚本的函数

[英]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: 我有以下指令:

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 我想在这方面做些事情,但这没用

render (function that should return a script) 渲染(应该返回脚本的函数)

function render(params){
   return (function(){
            $('#chart').append('<p> params </p>');
          })();
}

error 错误

rev is not defined

${}环绕render()调用

$element.append(`<script>${render(rev)}</script>`);

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

相关问题 如何编写返回JavaScript错误的函数 - How to write a function that returns an error in javascript 编写一个JavaScript函数,该函数返回包含您的姓名的字符串 - write a JavaScript function that returns a string that contains your name Javascript:返回承诺的写函数 - Javascript: write function that returns a promise 我如何编写一个接受 10 个字母的字符串并在 Javascript 中返回相应电话号码字符串的 function? - How do i write a function that accepts a 10-character string of letters and returns a corresponding phone number string in Javascript? 在javascript中将函数写为字符串 - Write function as a string in javascript 如何在字符串中编写javascript函数作为字符串? - how to write javascript function as string in php? 如何在字符串中编写javascript函数 - How to write a javascript function inside a string 如何编写一个将字符串的子集作为数组返回的 javascript 程序? - How to write a javascript program that returns the subset of a string as an array? 如何编写返回数组内的对象作为输出的 Javascript function - How to write a Javascript function that returns objects inside an array as outputs 我必须写一个 javascript function 接受一个字符串,如果“字符串以元音开头和结尾”则返回 true - I have to write a javascript function that takes in a string and returns true if "the string starts and ends with a vowel"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM