angular2.0数据绑定语法


Data direction Syntax Binding type
One-way
from data source
to view target
COPY CODE
{{expression}}
[target] = "expression"
bind-target = "expression"
Interpolation
Property
Attribute
Class
Style
One-way
from view target
to data source
COPY CODE
(target) = "statement"
on-target = "statement"
Event
Two-way
COPY CODE
[(target)] = "expression"
bindon-target = "expression"
Two-way
例子:


 [disabled]="isUnchanged">Save

Binding targets

The target of a data binding is something in the DOM. Depending on the binding type, the target can be an (element | component | directive) property, an (element | component | directive) event, or (rarely) an attribute name. The following table summarizes:

Binding type Target Examples
Property Element property
Component property
Directive property
COPY CODE
 [src] = "heroImageUrl">
 [hero]="currentHero">
 [ngClass] = "{selected: isSelected}">
Event Element event
Component event
Directive event
COPY CODE
 (click) = "onSave()">Save
 (deleteRequest)="deleteHero()">
 (myClick)="clicked=$event">click me
Two-way Event and property
COPY CODE
 [(ngModel)]="heroName">
Attribute Attribute (the exception)
COPY CODE
 [attr.aria-label]="help">help
Class class property
COPY CODE
 [class.special]="isSpecial">Special
Style style property
COPY CODE
 [style.color] = "isSpecial ? 'red' : 'green'">

Let’s descend from the architectural clouds and look at each of these binding types in concrete detail.


你可能感兴趣的:(angular2.0)