标签:template-literals javascript angular typescript ecmascript-6
在我的Angular 4应用程序中,我有一个接受字符串输入的组件:
<app-my-component [myInput]="'some string value'"></app-my-component>
在某些情况下,我需要在字符串中传递一个变量,例如:
<app-my-component [myInput]="'My name is ' + name + '!'"></app-my-component>
如果我可以使用es6 template literals(也就是模板字符串或反向字符串)会很好:
<app-my-component [myInput]="`My name is ${name}!`"></app-my-component>
但它不起作用:
Uncaught Error: Template parse errors:
Parser Error: Unexpected token Lexer Error: Unexpected character [`] at column 1 in expression
完成它的正确方法是什么?
解决方法:
ES6模板文字不能在Angular组件输入中使用,因为Angular编译器不知道这个语法.
这样你提供的方式很好.
<app-my-component [myInput]="'My name is ' + name + '!'"></app-my-component>
或类似的东西,
在组件中,
// In the component, you can use ES6 template literal
name: string;
input: string;
ngOnInit() {
this.name = 'some name;
this.input = `My name is ${this.name}!`;
}
在HTML中,
<app-my-component [myInput]="input"></app-my-component>
标签:template-literals,javascript,angular,typescript,ecmascript-6 来源: https://codeday.me/bug/20191006/1860884.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。