(based on how they interact with the JavaScript data in scopes)
- One-way binding
- String interpolation — render data, not modify data,
so-called "binding expression"
- Bind model (component class) to view (DOM)
using
{{property-name-from-model}}
- Should be used when dealing with string expression
- Example: in view, binding-example.component.html
<h1>{{ title }}</h1>
in component, binding-example.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-binding-example',
templateUrl: './binding-example.component.html',
styleUrls: ['./binding-example.component.css']
})
export class BindingExampleComponent implements OnInit {
constructor() { }
title = 'Data binding example';
ngOnInit() {
}
}
in view, app.component.html, include the following line
<app-binding-example></app-binding-example>
- Another example, in an order data model (order.ts class)
export class Order {
constructor(
public name: string,
public email: string,
public phone: number,
public drink: string,
public tempPreference: string,
public sendText: boolean,
){}
}
in view, app.component.html, to access the email property of the data model,
{{ orderModel.email }}
to access the data model and display the data in JSON format,
{{ orderModel | json }}
(assuming an instance of the order data model, named
orderModel, has been created in app.component.ts)
- Property or attribute binding — render data, not modify data,
set an attribute property of a view element
- Bind model (component class) to view (DOM)
using
[HTML-attribute-name] = "property-name-from-model"
- Should be used when dealing with non-string expression
- Example: in view, binding-example.component.html
<input type="text" name="content" [value]="msg" />
in component, binding-example.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-binding-example',
templateUrl: './binding-example.component.html',
styleUrls: ['./binding-example.component.css']
})
export class BindingExampleComponent implements OnInit {
constructor() { }
msg = 'Duh';
ngOnInit() {
}
}
in view, app.component.html, include the following line
<app-binding-example></app-binding-example>
- Event binding — not render data, may modify data
- Update or send the value information of a certain variable from
view (DOM) to the model (component class)
using
(event-name) = "function-to-handle-the-event()"
- Example: in view, binding-example.component.html
<h1>{{ title }}</h1>
<button (click)="changeTitle()">Change title on click of this button</button>
in component, binding-example.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-binding-example',
templateUrl: './binding-example.component.html',
styleUrls: ['./binding-example.component.css']
})
export class BindingExampleComponent implements OnInit {
constructor() { }
title = 'Data binding example';
ngOnInit() {
}
changeTitle() {
this.title = 'Event binding';
}
}
in view, app.component.html, include the following line
<app-binding-example></app-binding-example>
- Class binding — not render data, not modify data,
set a class property of a view element, using
[class.css-class-selector]="optional-under-which-conditions-to-apply"
- Style binding — not render data, not modify data,
set a style of a view element, using
[style.css-property]="value"
- Two-way binding
- Combine property and event binding,
using
ngModel
directive
[(ngModel)] = "property-name-from-model
- Changes made in the component's data synch to the view;
changes made in the view immediately update the model (the component's data)
- Mainly used in data entry forms
- Example: in view, binding-example.component.html
<input type="text" name="content" [(ngModel)]="msg" />
in component, binding-example.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-binding-example',
templateUrl: './binding-example.component.html',
styleUrls: ['./binding-example.component.css']
})
export class BindingExampleComponent implements OnInit {
constructor() { }
msg = 'Duh';
ngOnInit() {
}
}
in view, app.component.html, include the following line
<app-binding-example></app-binding-example>
- Another example, in an order data model (order.ts class)
export class Order {
constructor(
public name: string,
public email: string,
public phone: number,
public drink: string,
public tempPreference: string,
public sendText: boolean,
){}
}
in view, app.component.html, to access the email property of the data model,
<input [(ngModel)]="orderModel.email"
type="email" class="form-control" name="email">
(assuming an instance of the order data model, named
orderModel, has been created in app.component.ts)