Javascript 如何将变量从typescript传递到嵌入式JS?
我在angular项目中使用JavaScript,无法使用变量来实现动态行为,有没有办法使用从服务接收的this.testID,然后将其传递给下面的JS代码,我当前的静态值为1234。请告诉我您的建议/解决方案Javascript 如何将变量从typescript传递到嵌入式JS?,javascript,angular,typescript,Javascript,Angular,Typescript,我在angular项目中使用JavaScript,无法使用变量来实现动态行为,有没有办法使用从服务接收的this.testID,然后将其传递给下面的JS代码,我当前的静态值为1234。请告诉我您的建议/解决方案 public ngOnInit() { this.testID = this.someSettings.testID; // this is the ID to be passed to below JS } public onClicked() { let
public ngOnInit() {
this.testID = this.someSettings.testID; // this is the ID to be passed to below JS
}
public onClicked() {
let script = this._renderer2.createElement('script');
script.type = `text/javascript`;
script.text = `{
window._fs = window._fs || [];
_fs.push(['cid', 'something']);
_fs.push(['uid', '1234']); // here is where I want the ID from above to be passed instead of 1234
}`;
this._renderer2.appendChild(this._document.body, script);
}
谢谢您您的整个
脚本。文本
是反勾号,即一种支持多行和解释值的字符串类型:
script.text = `{ // <--- Starting backtick
window._fs = window._fs || [];
_fs.push(['cid', 'something']);
_fs.push(['uid', ${this.testID}]); // <-- Interpreted value
}`; // <--- Ending backtick
script.text=`{/\u fs.push(['uid',this.testID])
?这不起作用,我已经试过了。它在一个反勾字符串中,试一下`u fs.push(['uid',${this.testID}])`
@JeremyThille这会导致它无法识别出[u fs],它说“找不到名称”\u fs.”你不需要反勾号是什么意思?你的字符串已经在反勾号中,你自己使用反勾号发布了我的解决方案