【发布时间】:2018-05-21 15:58:53
【问题描述】:
我是 ngOnchanges 的新手,面临以下问题。
我的父组件在 ngOnChanges 上设置推荐值并将相同的值发送到子组件。 child 接收与 ngOnChanges 中的输入相同的内容。根据某些条件,例如 totalVal>0,我将 inputField 设置为 true,最初设置为 false。如果 inputField 为真,我会以反应形式显示一些组件。但是当我的以下结构执行 model2Form() 时,它仍然将 inputField 设为 false。我无法发布实际代码,所以只是根据我的项目创建了一个结构,如下所示。
请建议我如何解决这个问题。
// parent component
ngOnchanges(){
this.recom = [{
totalVal: 5000,
monthlydata: true
}]
}
//child component
@Input()
private recom: any;
inputField: boolean = false;
ngOnChanges(){
this.setValue();
this.loadFuture();
}
setValue(){
if(this.recom.totalVal > 0){
this.inputField = true;
}
}
loadFuture(){
}
model2Form(){
//getting input field as false even if i set it as true in setValue()
if(inputField){
this.heroForm.setControl('secretLairs', addressFormArray);
}
}
<!-- parent component>
<parent-comp [recom]="recom"></parent-comp>
<!-- child component -->
<div [FormControlName]="secretLairs"> </div>
【问题讨论】:
-
能否在 jsfiddle 提供您的代码
-
对不起,兄弟,我在 VDI 工作,所以不能在这里复制/粘贴代码,所以在这里创建了一个副本
-
你的父母 ngChange 正在工作,但孩子没有工作......对吗??
-
让我知道 setValue(){ 在更改完成时被调用???
-
已提供答案,请看一下
标签: angular