Now we are going to talk about model driven forms, we already discussed what template driven form is in our previous article,
I have opened the same solution that I explained in my previous article, and I have app.component.ts with onsubmit function and I have html elements in app.component.html.
Now we are going to convert template driven form into model driven forms; model driven forms are more powerful and more flexible.
There are some things that we can't do with template driven form but with model driven form we can perform those things. Like if we want to detect if any changes occur in any variable we can do it with model driven form , we can easily do two-way data binding with model drivven forms and that is very important in large applications.
So let's do it and convert it into model driven form now.
You can see that in app.module.ts file, we have FormsModule in it, replace it with ReactiveFormsModule as below,
Before
After
Now lets come back to app.component.ts file,we need to import two more libraries
Now we are going to convert template driven form into model driven forms; model driven forms are more powerful and more flexible.
There are some things that we can't do with template driven form but with model driven form we can perform those things. Like if we want to detect if any changes occur in any variable we can do it with model driven form , we can easily do two-way data binding with model drivven forms and that is very important in large applications.
So let's do it and convert it into model driven form now.
You can see that in app.module.ts file, we have FormsModule in it, replace it with ReactiveFormsModule as below,
Before
- import {
- BrowserModule
- } from '@angular/platform-browser';
- import {
- NgModule
- } from '@angular/core';
- import {
- FormsModule
- } from '@angular/forms';
- import {
- AppComponent
- } from './app.component';
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- FormsModule
- ],
- bootstrap: [AppComponent]
- })
- export class AppModule {}
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { ReactiveFormsModule } from '@angular/forms';
- import { AppComponent } from './app.component';
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- ReactiveFormsModule
- ],
- bootstrap: [AppComponent]
- })
- export class AppModule{}

Sagar Pandurang KapPosted Jan 15, 2018, 11:51 PM
Very nice article.Keep SHaring...