Data Binding in Angular

 

3. What is data binding? Which type of data binding does Angular deploy?

Data binding is a phenomenon that allows any internet user to work with dynamic data. It uses dynamic HTML and makes use of simple scripting. We use data binding in web pages that contain interactive components such as forms, calculators, tutorials, and games. When a page has lots of data, incremental display of data becomes more important from performance point of view.

Angular uses the two-way binding. Any changes made to the user interface are reflected in the corresponding model state. Conversely, any changes in the model state are reflected in the UI state. This allows the framework to connect the DOM to the Model data via the controller. However, this approach affects performance since every change in the DOM has to be tracked

Example

<app-sizer [(size)]="fontSizePx"></app-sizer>

For two-way data binding to work, the @Output() property must use the pattern, <input>Change, where input is the name of the @Input() property. For example, if the @Input() property is size, the @Output() property must be sizeChange.
The following sizerComponent has a size value property and a sizeChange event. The size property is an @Input(), so data can flow into the sizerComponent. The sizeChange event is an @Output(), which lets data flow out of the sizerComponent to the parent component

 

Next, there are two methods, dec() to decrease the font size and inc() to increase the font size. These two methods use resize() to change the value of the size property within min/max value constraints, and to emit an event that conveys the new size value

export class SizerComponent { @Input() size!: number | string; @Output() sizeChange = new EventEmitter<number>(); dec() { this.resize(-1); } inc() { this.resize(+1); } resize(delta: number) { this.size = Math.min(40, Math.max(8, +this.size + delta)); this.sizeChange.emit(this.size); } }

Comments

Popular posts from this blog

Hibernate Many to Many Relationship

Why Integral Calculus limit tending to infinity a sacrilege

Introduction