Angular2, evaluate template from string inside a component

前端 未结 3 772
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 13:32

It\'s possible evaluate template from string in a variable?. I need place the string in the component instead of the expression, e.g.

template: \"

3条回答
  •  清歌不尽
    2020-12-01 13:59

    In Angular double curly braces {{}} are used to evaluation an expression in a component's template. and not work on random strings or dynamically added DOM elements. So one way of doing this is to use typescript string interpolation using ${}. check the rest of code to understand

    @Component({
      selector: 'product-item',
      template: `
        
    `, }) export class ProductItemComponent { @Input() name: string; @Input() price: number = 0; @Input() template: string = `${ this.name } ${ this.price }}`; }

提交回复
热议问题