1
votes

I have an angular 2 model-driven form that has a property of type "FormArray". When I add or remove items to this form array the form itself does not seem to register any changes and the form is not made "dirty".

Here is a plunker showing the problem: https://plnkr.co/edit/j1S80CmPBF1iHI5ViEia?p=preview

Here is the method that removes an element from the FormArray:

removeAddress(i: number) {
    const control = <FormArray>this.myForm.controls['addresses'];
    control.removeAt(i);
}

Is there a way other than manually setting this.myForm.dirty = true for the form to detect that changes have been made to this <FormArray> property?

2

2 Answers

1
votes

Normally , dirty means there has been some value update in any of the fields .

An input is dirty , only if user has started typing ( has made it dirty) .

But if you want to hack this altogether and make it dirty yourself after you add or remove any element :

If you want to make the form dirty :

       this.myForm.valueChanges.subscribe(()=>{

                this.myForm.markAsDirty();
       });
0
votes

AngularJS2 added back the concept of two way binding.

In AngularJS2 you need to explicitly indicate that the control has two way binding.

https://angular.io/docs/ts/latest/guide/forms.html

[(ngModel)]="model.name" name="name"

Should give you what you want.