angular2.0数据绑定语法


Data direction Syntax Binding type
One-way
from data source
to view target
{{expression}}
[target] = "expression"
bind-target = "expression"
Interpolation
Property
Attribute
Class
Style
One-way
from view target
to data source
(target) = "statement"
on-target = "statement"
Event
Two-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 type Target Examples
Property Element property
Component property
Directive property
<img [src] = "heroImageUrl">
<hero-detail [hero]="currentHero"></hero-detail>
<div [ngClass] = "{selected: isSelected}"></div>
Event Element event
Component event
Directive event
<button (click) = "onSave()">Save</button>
<hero-detail (deleteRequest)="deleteHero()"></hero-detail>
<div (myClick)="clicked=$event">click me</div>
Two-way Event and property
<input [(ngModel)]="heroName">
Attribute Attribute (the exception)
<button [attr.aria-label]="help">help</button>
Class class 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值