【发布时间】:2019-05-23 19:11:36
【问题描述】:
我正在尝试通过修改标准 stackblitz.com“Hello”项目来学习如何使用 ng-template,以便 Hello 组件由 ng-template 呈现。不幸的是,它得到了一个讨厌的 ExpressionChangedAfterItHasBeenCherredError。
上一个值:'名称:未定义'。当前值:“名称:Angular”。看起来视图是在其父级及其子级经过脏检查后创建的。它是在变更检测钩子中创建的吗?
有人可以解释一下这可以在没有错误的情况下完成吗?
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular';
@ViewChild('tpl') tpl;
constructor(private _vcr: ViewContainerRef) { }
ngAfterViewInit() {
this._vcr.createEmbeddedView(this.tpl);
}
}
app.component.html:
<ng-template #tpl>
<hello name="{{ name }}"></hello>
</ng-template>
链接:https://stackblitz.com/edit/angular-48ptik?file=src%2Fapp%2Fapp.component.html
【问题讨论】:
标签: angular ng-template