I made a dynamic field form in which the user can add more fields. I want to detect the changes in libId But the changes are only detecting for the first field.
My code-
this.invoiceForm = this._fb.group({
itemRows: this._fb.array([this.initItemRows()]) // here
});
(<FormArray>this.invoiceForm.get('itemRows')).controls.forEach(
control => {
console.log('reaches');
control.get('libId').valueChanges
.pipe(
distinctUntilChanged()
)
.subscribe(value => console.log(value));
}
);
initItemRows() {
return this._fb.group({
// list all your form controls here, which belongs to your form array
libId: [null, Validators.required],
rollNumber: [null, Validators.required]
});
}
addNewRow() {
// control refers to your formarray
const control = <FormArray>this.invoiceForm.controls['itemRows'];
// add new formgroup
control.push(this.initItemRows());
}
I am using this code inside the ngOnInit. I don't know why changes are not being detected for any other field.
Please suggest some solutions.
(change)event on theinput?