Using Angular 2, two-way binding is easy in template-driven forms - you just use the banana box syntax. How would you replicate this behavior in a model-driven form?
For example, here is a standard reactive form. Let's pretend it's much more complicated than it looks, with lots and lots of various inputs and business logic, and therefore more appropriate for a model-driven approach than a template-driven approach.
export class ExampleModel { public name: string; // ... lots of other inputs } @Component({ template: ` <form [formGroup]="form"> <input type="text" formControlName="name"> ... lots of other inputs </form> <h4>Example values: {{example | json}}</h4> ` }) export class ExampleComponent { public form: FormGroup; public example: ExampleModel = new ExampleModel(); constructor(private _fb: FormBuilder) { this.form = this._fb.group({ name: [ this.example.name, Validators.required ] // lots of other inputs }); } this.form.valueChanges.subscribe({ form => { console.info('form values', form); } }); }
In the subscribe()
I can apply all sorts of logic to the form values and map them as necessary. However, I don't want to map every input value from the form. I just want to see the values for the entire employee
model as it updates, in a approach similar to [(ngModel)]="example.name"
, and as displayed in the json pipe in the template. How can I accomplish this?
We can perform Two way binding in Template driven forms but there is no two way binding in Reactive forms. Angular provides the methods to update the values from the component class. Reactive forms are used on complex cases, like dynamic forms element, dynamic validations etc.
You can use [(ngModel)] with Reactive forms. This will a completely different directive than the one that would be used without the formControlName . With reactive forms, it will be the FormControlNameDirective . Without the formControlName , the NgModel directive would be used.
Two-way binding gives components in your application a way to share data. Use two-way binding to listen for events and update values simultaneously between parent and child components.
The two-way data binding in Angular is used to display information to the end user and allows the end user to make changes to the underlying data using the UI. This makes a two-way connection between the view (the template) and the component class. This is similar to the two-way binding in WPF.
Note: as mentioned by @Clouse24, "Using Reactive Froms with ngModel is deprecated in angular 6 and will be removed in a future version of Angular" (which means that the answer below will no longer be supported in the future). Please read the link to see the reasoning for deprecation and to see what alternatives you will have.
You can use [(ngModel)]
with Reactive forms.
template
<form [formGroup]="form"> <input name="first" formControlName="first" [(ngModel)]="example.first"/> <input name="last" formControlName="last" [(ngModel)]="example.last"/> </form>
component
export class App { form: FormGroup; example = { first: "", last: "" }; constructor(builder: FormBuilder) { this.form = builder.group({ first: "", last: "" }); } }
Plunker
This will a completely different directive than the one that would be used without the formControlName
. With reactive forms, it will be the FormControlNameDirective
. Without the formControlName
, the NgModel
directive would be used.
Sometimes you might need to combine [(ngModel)] with Reactive forms. I could be some inputcontrol that you don't need as a part of the form, but you still need it to be binded to the controller. Then you can use: [(ngModel)]="something" [ngModelOptions]="{standalone: true}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With