×

Data flow forms in Angular 8

When we are building forms in Angular, it is essential to understand how the framework handles data flowing from the user or programmatic changes. Reactive and template-driven forms follow two different strategies when handling form input.

Data Flow in Reactive Forms

In Reactive form, each form of elements in the view is linked to a form model (form Control instance). Updates from the view to the model and from the model to the view and synchronous. And aren't dependent on the UI rendering. The diagram below uses the same favorite color example to demonstrate how data flows work when an input fields value is changed from the view and then from the model.

data flow forms in angular 8

The steps of the data flow from view to model:

1. The user types a value in the input element; in the case, the favorite color is Blue.

2. The form input element releases an "input" event with the latest value.

3. The control value accessor attends for functions on the form input element immediately relay the new value to the FormControl instance.

4. The form control instance releases the original amount through the value changes observables.

5. Any subscriber to the valueChanges observable receives the new value.

data flow forms in angular 8 1

The steps of the data flow from model to view:

  1. The user calls the favoritecolorcontrol.setvalue method. Which update the form control value.
  2. Then the form control instance recovers the new value through the value changes observables.
  3. Then subscribers to the valueChanges observables receive the new value.
  4. At least, the control value accessor on the form input element update the elements in the new value.

Data Flow in Template-Driven Form

In template-driven form, each form element is linked to a directive that manages the form model internally. The diagram below uses the same favorite color example to demonstrate how data flows when any input field's value is changed into the view and from the model.

Data flow from View to model

data flow forms in angular 8 2

The steps in the data flow from view to model in reactive forms:

Here, the users have to change the input value from red to blue.

  • Firstly, the user types Blue in the input element.
  • Then, the input element produces an "input" event possess the value Blue.
  • Then, the control value accessor connect to the input triggers in the setValue() method on the FormControl instance.
  • The FormControl instance emits the new value by the value changes observable.
  • Any subscribers to the valueChanges observable obtain the new value.
  • The control value accessor also announce the Ngmodel.viewToModelUpdate() method which emits an ngModelChange event.
  • The component template uses two-way data binding for the favoritecolor property, the favoriteColor property in the component is updated in the value emitted by the ngModel! Change event (Blue).

Data flow from Model to View

data flow forms in angular 8 3

The steps in the data flow from model to view in reactive forms:

The data flow from model to see contained the following steps when the favoriteColor elements change from red to blue.

  • Previously, the favoriteColor is updated to a new value in the component.
  • Change detection creates.
  • During change detection, the ngOnChanges lifecycle hook has been called in the NgModel directive instance because the value of its inputs has changed.
  • The ngOnChanges( ) methods queue an async task to set the value to the internal FormControl instance.
  • Change detection is executed now.
  • Then, the task to fix the FormControl instance value is implemented.
  • The Fromcontrol instance emits the latest valueChanges observable.
  • Any subscribers to the valueChanges observable accept the new value.
  • The control value accessor updates from the input element in the view with the latest favoriteColor value.

Related Topics

Angular 8 ngIf Directive

ngIf Directive is the part of structural directives.The NgIf is the most straightforward structural Directive and comfortable to understand. The ngIf Directives is used to add and remove HTML elements according to...

2 minutes read.

History and Versions of Angular 8

Introduction to Angular JS Misko created Angular JS and the first version of Angular also name as "Angular 1". He built a framework to handle the downfalls of HTML. The first...

3 minutes read.

Angular 8 *ngFor Directive

Angular 8ngFor Directive The *ngFor directive is used to repeat to repeat a portion of HTML template once per each item from an iterable list (collection). The ngFor is an Angular...

2 minutes read.

Angular 8 Tutorial

What is Angular 8 Angular is a Framework of JavaScript used to build web and mobile applications. Angular 8 is a client-side TypeScript based structure which is used to create dynamic...

6 minutes read.

Angular 8 Unit Testing

What is unit testing? Unit testing is a type of software testing where individual components of the software are tested. It is done during the development of any application. A unit may be...

6 minutes read.

Angular 8 universal

Angular 8 universal Angular Universal is a technology that allows server-side rendering for Angular apps. An angular app is a single-page app, and it runs in a client-side browser. Rendering pages in the DOM is...

6 minutes read.

Property binding in Angular 8

Property binding is the primary way to binding data in Angular.  To bind data to a property of any element, we use square braces[ ]. It is also a one-way data-binding...

2 minutes read.

Angular 8 Libraries

Angular libraries are built a solution of general problems like presenting a unified user interface, presenting data, and allowing the data entry. Developers can create standard solutions for particular domains...

3 minutes read.

Advantages | Disadvantages of Angular 8

Advantages of Angular 8 There are some Advantages of angular 8 which are given below: It offers clean code developmentHigher PerformanceAn angular framework can take care of routing, which...

1 minute read.

ngSwitch Directive in Angular 8

Angular 8 ng-Switch Directive The ng-Switch Directive hides and shows the HTML elements depending on an expression. Child elements with the ng-switch-when directive will be displayed if it gets a match; otherwise, the component and...

2 minutes read.

Express.js app.get() Request Function

Express.js Express is a flexible online application framework for Node.js that offers a strong set of functionality for both web and mobile applications. While preserving the familiar and adored Node.js features,...

4 minutes read.

Routing in Angular 8

Routing Angular Router is a powerful JavaScript router is built and maintained by the Angular core team that can install from the package @angular/router. Routing provides a complete routing library with the possibility of multiple...

4 minutes read.

Angular 8 Observables

Observables provide support for passing messages between publisher and subscription in our application. The observables can deliver the multiple values of any type like literal, messages, depending on the content. It is an...

6 minutes read.

Angular 8 File Structure

File Structure in Angular 8: A file structure contains the files for one or more projects. A project is the set of files that comprise an application on a shareable...

3 minutes read.

Installation of Angular 8

Installation Guides to Angular 8 step by Step Angular combines declarative templates, dependency injection and, end to end tooling to solve development challenges. Angular empowers developers to build an application that lives on the...

4 minutes read.

Angular 8 Forms

Angular forms are used to handle the user's input. We use Angular form in our application to authorize users to log in, to update profile, to enter information, and to perform many other...

3 minutes read.

Angular 8 Directives

Angular 8 Directives: Directives are instructions in the DOM (Document Object Model). It specifies how to place our business logic in Angular. The directive is markers on a DOM element...

3 minutes read.

Dependency Injection in Angular 8

Dependency injection (DI), is an essential application design pattern. Angular 8 has its own DI framework, which used in the design of Angular application to increase efficiency and portability. Dependencies are the services that...

7 minutes read.

Creating first APP in Angular 8

Firstly, we have to open Git Bash and, then we have to write the following command in it. ng new my -app The project has been created now so that now we have to go...

2 minutes read.

Angular 8 Architecture

Architecture of Angular 8 Angular 8 is a platform and framework which is used to create clients applications in HTML and Typescript. Angular 8 is written in Typescript. Typescript is a...

5 minutes read.