有人可以帮我解决这个问题吗?当表单有效时,但是我返回并初始化此组件(将新值推送到 FormArray 中(从零项开始))时会出现此问题。如何找出哪个属性触发了 ng-valid?是数组吗?数组中的单个元素?还是整个 formgroup?
我尝试过ngAfterContentChecked() { this.cdr.detectChanges(); }和changeDetection: ChangeDetectionStrategy.OnPush,并在 push 调用后调用this.cdr.detectChanges()。仍然无法解决。很奇怪,因为它只发生在表单第一次有效,然后我添加一个元素。
我尝试过ngAfterContentChecked() { this.cdr.detectChanges(); }和changeDetection: ChangeDetectionStrategy.OnPush,并在 push 调用后调用this.cdr.detectChanges()。仍然无法解决。很奇怪,因为它只发生在表单第一次有效,然后我添加一个元素。
@Component({
selector: 'app-list-other-conditions',
templateUrl: './list-other-conditions.component.html',
styleUrls: ['../survey-pmhx.component.scss']
})
export class ListOtherConditionsComponent implements OnInit, OnDestroy {
@Input()
formGroup!: FormGroup
@Input()
arrayName!: string
get formArray():FormArray {
return this.formGroup!.get(this.arrayName)! as FormArray
}
addItem() {
this.formArray.push(this.initItem())
}
removeItem(i:number) {
this.formArray.removeAt(i)
if (this.formArray.length === 0)
this.formArray.push(this.initItem())
}
public initItem = () : FormGroup =>
this.fb.group({
diagnosis: this.fb.control(null, Validators.required),
year: this.fb.control(null, [Validators.required, CustomValidators.pastYear])
})
constructor(private fb: FormBuilder, private readonly cdr: ChangeDetectorRef) { }
ngOnInit(): void {
if (this.formArray.length === 0)
this.addItem();
}
}
}
<form [formGroup]="form"
let myform = this.fb.group({ list: this.fb.array([]))})
。而这个子组件被称为<app-list-other-conditions [formGroup]="myform" arrayName="list"></...
。 - user17842237