【发布时间】:2020-06-03 14:55:18
【问题描述】:
我遵循了这个solution,在传单弹出窗口中创建动态组件。我可以渲染我的组件。但是当我尝试使用 *ngFor 得到以下错误
我的父组件代码来呈现弹出窗口
let popup = L.popup();
this.zone.run(() => {
if (this.componentRef) {
this.componentRef.destroy();
}
const compFactory = this.resolver.resolveComponentFactory(
LeafletPopupComponent
);
this.componentRef = compFactory.create(this.injector);
if (this.appRef["attachView"]) {
// since 2.3.0
this.appRef["attachView"](this.componentRef.hostView);
this.componentRef.onDestroy(() => {
this.appRef["detachView"](this.componentRef.hostView);
});
} else {
this.appRef["registerChangeDetector"](
this.componentRef.changeDetectorRef
);
this.componentRef.onDestroy(() => {
this.appRef["unregisterChangeDetector"](
this.componentRef.changeDetectorRef
);
});
}
this.componentRef.instance.parent_text = "msg from parent";
this.componentRef.instance.items = [
"workstation 1",
"workstation 2"
];
let div = document.createElement("div");
div.appendChild(this.componentRef.location.nativeElement);
popup.setContent(div);
});
this.markerObj.bindPopup(popup).addTo(this.map);
动态组件html代码
<div class="container">
<h5>Step 1</h5>
<span>Name the selected type of workstation</span>
{{ parent_text }}
<div class="form-group w-100" *ngIf="items?.length">
<select class="form-control">
<option value="">Select</option>
<option *ngFor="let item of items" [value]="item">
{{item}}
</option>
</select>
</div>
<h5>Step 2</h5>
<a> Mark Area <i class="fa fa-pencil"></i></a>
</div>
动态组件ts文件
export class LeafletPopupComponent{
@Input() items;
@Input() parent_text: string;
constructor() {}
}
注意我可以成功地从父组件渲染 parent_text 消息。 请帮忙。
【问题讨论】: