Angular 5 - ComponentFactoryResolver和属性

问题描述 投票:1回答:1

您好我正在尝试通过ComponentFactoryResolver渲染组件视图并将其放到DOM中,但是在渲染时未定义属性,在这种情况下该怎么办?

组件中的属性:

@Input('productId') productId;

创建视图并将数据传递给属性:

const componentRef: any = this.componentFactoryResolver
  .resolveComponentFactory(component)
  .create(this.injector);
componentRef.instance.productId = data.productId;

并在模板中

<div>{{ productId }}</div>

productId未定义

angular typescript angular2-template angular5
1个回答
2
投票

呼叫

componentRef.changeDetectorRef.detectChanges();

修改属性以调用更改检测后。

另见https://angular.io/api/core/ComponentRef#changeDetectorRef

© www.soinside.com 2019 - 2024. All rights reserved.