您的位置:首页 > 运维架构

[Angular 2] Property Binding

2016-04-06 03:23 405 查看
Property Binding is bind property NOT attribute!

import {Component, Input, Output, EventEmitter} from 'angular2/core';

@Component({
selector: 'hero-item',
styles: [
'.active {color: red}'
],
template: `
<li [class.active]="isSelected"
[attr.aria-label]="hero.name"
(click)="selectHero(hero)">
{{hero.name}}
</li>
`
})
// <li [ngClass]="{active: isSelected}"></li>

export class HeroItem{
label="This is a super hero";
isSelected = false;
@Input() hero ;
@Output() changed = new EventEmitter();
constructor(){

}

selectHero(hero){
this.changed.emit(hero);
this.isSelected = true;
}
}


So 'class' is attribute on DOM, but 'class.active' is an property.

'aria-label' is attribute, so need to write like 'attr.aria-label'.

If you don't like use 'class.active', you can use 'ngClass' as shown in commnets
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: