5. Property Binding in Angular
Property Binding in Angular
What is Property binding ?
1. It is one type of one way data binding from Controller to View but can be used on the properties of HTML elements.
2. This binding can not be used between the HTML elements like interpolation to display the value's.
3. This can be indicated using [propertyName] ="Variable in controller".
Explanation with a sample program
Create a component using following command
1. Open command prompt or Terminal in Visual studio and type
ng generate component sample --spec=false
The above command creates 3 files
(a). sample.component.html(b). sample.component.ts
(c). sample.component.scss
Code in sample.component.html
<input [type]="typeValue" />
sample.component.ts
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-sample', templateUrl: './sample.component.html', styleUrls: ['./sample.component.scss'] }) export class SampleComponent implements OnInit { typeValue = 'password'; constructor() { } ngOnInit() { } }
Explanation
1. The typeValue inside the controller will bind to "type" property of the input field.
2. As "typeValue" given is password the field renders with "password" type.
3. If the "typeValue" does'nt match with any of properties given by HTML it will render the default value of the property.
4. Where ever "typeValue" changes in a controller it effects the HTML content.
Comments
Post a Comment