Angular2, evaluate template from string inside a component

前端 未结 3 752
佛祖请我去吃肉
佛祖请我去吃肉 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

    not sure how you're building the template string

    import { ..., OnInit } from '@angular/core';
    
    @Component({
        selector: 'product-item',
        template: `
        
    `, }) export class ProductItemComponent implements OnInit { @Input() name: string; @Input() price: number = 0; @Input() pre: string; @Input() mid: string; @Input() post: string; template_string; ngOnInit() { // this is probably what you want this.template_string = `${this.pre}${this.name}${this.mid}${this.price}${this.post}` } }

    the string can be built from outside the component, would still recommend something like ngIf to control dynamic templates though.

提交回复
热议问题