【发布时间】:2020-09-02 07:15:35
【问题描述】:
我正在尝试创建一个子组件(它是一个表单),它出现在一个模态的父组件中。
表单(子组件)必须根据父级中选择的类型通过几个按钮(每个按钮是不同的类型)进行更改。
当你点击按钮时,子组件必须根据父组件中选择的类型来修改表单。表单控件正确更改,但模板未修改。代码如下:
打开modal并调用子组件的父方法
show(value: string){
this.type = value;
this.entityFormComponent.type = this.type;
this.entityFormComponent.ngOnInit();
// Set submitAttemp to false for the new form
this.entityFormComponent.submitAttemp = false;
this.contentModal.show();
}
子组件的方法ngOnInit
ngOnInit(): void {
if(this.creating){
this.entity = new Entity();
} else {
this.entityService.get(this.nameToEdit, this.type)
}
// Initialize the form and add the corresponding attributes according to the type
this.entityForm = new FormGroup({});
this.entityHelper.getEntityFormAttributes(this.entityForm, this.type, this.entity);
}
到目前为止,似乎一切都是正确的,并且每次单击父按钮时生成的表单组都足够了,但是表单的输入没有出现。它们是通过辅助方法生成的,该方法通过传入的输入的类型和属性返回真或假:
<div class="col-md-6" *ngIf="entityHelper.getEntityInputsAttributes('shadow', type)">
视图中的类型是“未定义”,但在组件中正确显示。为什么视图不能正确更新类型?我需要做什么来更新它?
【问题讨论】:
标签: javascript angular components