Friday, 22 March 2019

Angular Form, ngModel and Detecting Changes

Angular (at the moment of writing is version 8) has two flavours when coming down to forms. We can use Template Driven or Reactive Form. Template Driven form is useful for simple form that does not need much customisation. It is similar to the form in AngularJS. Angular will create form model representation and data binding in the background with some default structure and names. That is why it is called Template Driven.

Reactive Form, on the other hand is much more flexible however requires more effort in setting up. This form uses more code in component and less in HTML. Reactive form is best to handle complex scenarios and allow easier unit testing.

We are going to see an example of Template Driven form with ngModel binding.
<form name="personNameForm" novalidate="" ng-submit="submitForm()">
 <div>
  <input type="text" name="firstName" class="textinput"
                 [(ngModel)]="person.firstName"
                 (ngModelChange)="combineNames()"
                 required />
  <input type="text" name="lastName" class="textinput"
                 [(ngModel)]="person.lastName"
                 (ngModelChange)="combineNames()"
                 required />
 </div>
</form>
Note that we need to put form's name attribute and for each ngModel field a name attribute is also required.
We use ngModelChange directive to detect changes for NgModel value. $watch and $observe are not required anymore.

3 comments:

dean said...

The article provides a useful comparison between Angular Template-Driven Forms and Reactive Forms, particularly in terms of complexity, customization, and the amount of logic handled in the component versus the template. The example of using [(ngModel)] for two-way data binding and (ngModelChange) to detect changes provides a practical demonstration of how form values can be monitored and used to trigger application logic. For developers looking to build a stronger understanding of Angular forms and application development, an Angular Online Course can provide a structured learning path.

dean said...

The discussion of ngModelChange is particularly useful for understanding how changes to form values can be handled directly within an Angular component. The example of combining first and last names also demonstrates how data-binding events can be connected to application logic without relying on the approaches described for AngularJS. Since TypeScript is an important part of modern Angular development, a TypeScript Online Course can help developers strengthen the language fundamentals needed to work effectively with Angular components and models.

dean said...

Practical form examples such as this are valuable because forms are common in real-world applications, from registration and profile screens to data-entry workflows. Applying concepts such as ngModel, change detection, and form handling within complete applications can provide useful hands-on experience, and Angular Projects can provide opportunities to apply these frontend concepts in larger project implementations.