angular2.0数据绑定语法

来源:互联网 发布:字幕制作软件 编辑:程序博客网 时间:2024/04/28 01:19


Data directionSyntaxBinding typeOne-way
from data source
to view target

{{expression}}[target] = "expression"bind-target = "expression"
Interpolation
Property
Attribute
Class
StyleOne-way
from view target
to data source
(target) = "statement"on-target = "statement"
EventTwo-way
[(target)] = "expression"bindon-target = "expression"
Two-way例子:

<!-- Bind button disabled state to `isUnchanged` property --><button [disabled]="isUnchanged">Save</button>

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 typeTargetExamplesPropertyElement property
Component property
Directive property
<img [src] = "heroImageUrl"><hero-detail [hero]="currentHero"></hero-detail><div [ngClass] = "{selected: isSelected}"></div>
EventElement event
Component event
Directive event
<button (click) = "onSave()">Save</button><hero-detail (deleteRequest)="deleteHero()"></hero-detail><div (myClick)="clicked=$event">click me</div>
Two-wayEvent and property
<input [(ngModel)]="heroName">
AttributeAttribute (the exception)
<button [attr.aria-label]="help">help</button>
Classclass property
<div [class.special]="isSpecial">Special</div>
Stylestyle property
<button [style.color] = "isSpecial ? 'red' : 'green'">

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


0 0