码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular 2] Property Binding

时间:2016-04-06 07:05:16      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

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

[Angular 2] Property Binding

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5357667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!