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 technique. In property binding, we link property of a DOM element to a field. Angular internally converts string interpolation into property binding.
When a value in the data is change and update the view to reflect the changes it is detected by the Property binding, hence making the HTML dynamic.
Property binding is used to pass data to a component.
For example:
Property binding favor over string interpolation because it has short code. Which can be used when we want to display some dynamic data from a component on the view between headings. like h1, h2, p etc.
There are three types of property binding:
1. Component property binding
2. Element property binding
3. Directive property binding
Component property binding
It workswithin the component element to bind parent component property. In the diagram, the arrows and rectangles in red color are displaying the functionality related to component property binding.
Element property binding
Element property binding works within HTML element, and it binds a component property to a DOM property. In the diagram, the arrows and rectangle in green color are displaying the functionality related to element property binding.
Directive property binding
It also works within HTML element with directives such as Ngclass and NgStyle. In the directive, property binding a component properly or any angular expression is linked to the angular directive.
Property Binding Example
Open the app.component.ts file and add the below code:
import {component} from ‘@angular/core’; @Component ({ selector: ’app-root’, templateUrl:’./app.component.html’, styleUrls: [‘./app.component.css’] }) export class AppComponent { title= “Data binding using Property Binding”; imgUrl=https://server5.kproxy.com/servlet /redirect.srv /sruj/scxtachplrb/swdpxrr/p2/tutorial/angular7/images/angular-7-logo.png; }
Now, open app.component.html and use the below code for property binding
{{title}}
Run the ng serve command and open localhost 4200 to see the result.