angularmodel-driven-development

angular -Which form type to choose Model-Driven or Template Driven form?


when i use Model-Driven form like :

@Component({
  selector: 'my-app',
  template: `
  <p>
    <label>First Name:</label>
    <input type="text"  [ngModel]="user.firstName" required>
  </p>
  `,
  providers: [myService]
})

and when i use Template Driven form like :

 @Component({
      selector: 'my-app',
      template: `
      <p>
         <label>First Name:</label>
         <input type="text" formControlName="firstName">
      </p>
      `,
      providers: [myService]
    })

I think above both things do same functionality. so, I get little confusion which one is preferable while start new project ?


Solution

  • I think above both things do same functionality. so, I get little confusion which one is preferable while start new project ?

    You have not fully explored the forms as of now i guess . There are certain things which is hard or even not possible at present with template driven forms in Angular .

    Things like Form array adding and removing controls dynamically . Listing to Each control and doing stuff this is more practical and easy in Reactive Forms . And i feel going forward Angular team will switch completely to Reactive forms and that is way to go .

    Some Reasons i say so is - this PR to support NgModelArray in Template Driven Forms

    More on Reactive Forms and Template Driven Forms.

    Now its your call what you prefer.