我正在尝试动态创建一个具有要初始化的模型类的组件。
当我一次创建组件然后再次尝试时,它可以解决问题
这是我的代码:
.HTML
<div [formGroup]="articleCategoryGeneralBodyEditForm">
<input type="hidden" formControlName="category_details_language_id" [(ngModel)]="article_category_detail.category_details_language_id">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label for="category_details_name"><strong class="form-control-required">*</strong> Category Name</label>
<input type="text" class="form-control" id="category_details_name" placeholder="Enter category name | ex: Parent" formControlName="category_details_name" [(ngModel)]="article_category_detail.category_details_name">
</div>
</div>
</div>
</div>
.COMPONENT.TS
export class ElSubGeneralBodyEditComponent implements OnInit {
// Models
article_category_detail: ArticleCategoryDetail = new ArticleCategoryDetail();
// Form
articleCategoryGeneralBodyEditForm: FormGroup;
constructor(public _DataSharerService: DataSharerService, private _ToasterService: ToasterService, private _BlogArticleCategoryService: BlogArticleCategoryService) { }
setForm() {
this.articleCategoryGeneralBodyEditForm = new FormGroup({
category_details_name: new FormControl(),
category_details_language_id: new FormControl()
});
this.article_category_detail.category_details_language_id = this._DataSharerService.getLanguageId()[0];
}
ngOnInit() {
this.setForm();
}
}
这是我动态创建组件的方式:
createEditFormComponent() {
this.editFormContainerRef.clear();
const factory: ComponentFactory<any> = this.resolver.resolveComponentFactory(ElCategoriesEditComponent);
this.componentRef = this.editFormContainerRef.createComponent(factory);
this.componentRef.instance.thisComponentRef = this.componentRef;
}
这就是我销毁创建的组件的方式:
onClose() {
this.thisComponentRef.destroy();
}
/* Please be noted, ElCategoriesEditComponent is a parent component of ElSubGeneralBodyEditComponent */
我对其他组件也做过同样的工作,它们工作正常,只是其他组件之间的区别在于此组件(ElCategoriesEditComponent)的区别在于该组件具有子组件。
我知道很难描述这样的问题,或者也许我无法更清楚地说明这个问题,请问我是否需要更多信息。
任何帮助都会让我省一整天。
预先感谢!