Can someone help me troubleshoot this. It happens when the form becomes valid, but then I go back and init this component (push a new value in the FormArray (from zero items)). How do I figure out which property ng-valid is triggering on? Is it the array? individual element in array? or the entire formgroup?
I've tried ngAfterContentChecked() { this.cdr.detectChanges(); } and changeDetection: ChangeDetectionStrategy.OnPush, and calling this.cdr.detectChanges() after a push call. Still doesn't work. It's weird because it only happens the first time the form is valid, and then I add a an element.
@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();
}
}
}