![](/img/trans.png)
[英]javascript es5 Angular2 compontent templateurl relative path not working
[英]ES6 TemplateUrl like Angular2
我正在查看ES6,找到了我喜歡的模板字符串。
使用ES6模板字符串,我可以像這樣注入html:
$("#warning").html(`
<h1>Watch out!</h1>
<p>Unauthorized hockeying can result in penalties of up to 500 minutes.</p>
`);
反引號是ES6真正有用的功能。
在AngularJs2中,您可以使用TemplateUrl並實際導入外部頁面。
可以使用ES6(不使用Angular2)完成此操作嗎?
您的示例已經是ES6,但由於模板字符串會考慮所有空格,包括為了清楚起見而用於縮進代碼的空格,因此它並不總是有效。
您有兩種選擇。
使用字符串串聯:
$("#warning").html(
`<h1>Watch out!</h1>` +
`<p>Unauthorized hockeying can result in penalties of up to 500 minutes.</p>`
);
我的首選方法是數組連接:
$("#warning").html([
`<h1>Watch out!</h1>`,
`<p>Unauthorized hockeying can result in penalties of up to 500 minutes.</p>`
].join(''));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.