在Angular 7中,可以使用ngTemplateOutlet
指令来实现动态和多个ngTemplate的渲染。下面是一个示例代码:
Template 1
Template 2
@Component({
selector: 'app-example',
template: `...`
})
export class ExampleComponent {
template1: TemplateRef;
template2: TemplateRef;
constructor(private templateRef: TemplateRef) {
this.template1 = this.templateRef;
this.template2 = this.templateRef;
}
}
这样,当点击按钮时,对应的ngTemplate将被渲染到相应的位置。