[Angular 2] Property Binding

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

时间: 2024-08-25 00:20:42

[Angular 2] Property Binding的相关文章

[AngularJS - thoughtram] Exploring Angular 1.3: Binding to Directive Controllers

The post we have: http://www.cnblogs.com/Answer1215/p/4185504.html gives a breif introduce about bindToController on the directive. Here is a blog about bindToController from thoughtramwhich explains in detail why bingToController comes into handy an

Exploring Angular 1.3: Binding to Directive Controllers

原文: http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html Angular1.2引入了新的语法 controllerAs, 它让scope更加清楚.干净, 让controller更加聪明. 在我们的Angular应用中使用controllerAs可以避免开发者经常遇到的一些问题. controllerAs 做为命名空间 让我们用代码说事,有两个controller如下

Angular 2 Architecture Overview

Module 简单来说模块(module)就是完成共同目的的代码块,export一些内容例如一个类.函数.或值变量. component就是一个基本的Angular块,一个component类其实也是我们从模块中export出来的东西. Angular本身也是一个有着许多称为“barrels”的库模块的集合.angular2/core 是最主要的Angular库模块. 如果引入的是Angular本身的库模块,import语句直接引用的是模块的名称:如果引入的是自己本地的模块,则需要使用相对路径,

All About Angular 2.0

angular All About Angular 2.0 Posted by Rob Eisenberg on  November 6th, 2014. Have questions about the strategy for Angular 2.0? This is the place. In the following article I'll explain the major feature areas of Angular 2.0 along with the motivation

一次五分钟 angularJS (1)—— Binding

引用angularjs 需要使用AngularJS,需要引用AngularJS的文件 ng-app 要将angular用到页面绑定的时候,我们需要指明它的作用域. 在上图中,ng-app="" 表示我们现在演示的angular的作用域在该<div>标签之内.当然我们也可以将ng-app=""写在<body>标签上.写在哪个标签,我们的angular语法就支持在哪. 接下来,我们看看效果 可以看出,<div>中的{{2+2}}就被解

WPF BINDING

WPF里分三种Binding:Binding, PriorityBinding, MultiBinding,这三种Binding的基类都是BindingBase,而BindingBase又继承于MarkupExtension Binding 提供对绑定定义的高级别访问,绑定将绑定目标对象(通常为 WPF 元素)的属性与任何数据源(例如数据库.XML 文件或包含数据的任何对象)连接起来. 常见的使用Binding的代码:C# Binding binding = new Binding(); //

[WPF系列]基础 Listening to Dependency Property change notifications of a given Element

I want to share this great post from Anoop that shows a easy way to add a notification system to dependency properties of a given element. It creates and attaches a new property to the existing property and let's you specify the PropertyChangedCallba

Angular2版本更新

2.0.0-beta.0 somnambulant-inauguration (2015-12-15) Enjoy! 2.0.0-alpha.55 (2015-12-15) Bug Fixes router: export ROUTER_LINK_DSL_PROVIDER and hide MockPopStateEvent (fc75220) Features core: enable dev mode by default (3dca9d5) BREAKING CHANGES Before

Angular4.0踩坑之路:探索子路由和懒加载

参考文章: Angular4路由快速入门  http://www.jianshu.com/p/e72c79c6968e Angular2文档学习的知识点摘要--Angular模块(NgModule)http://lib.csdn.net/article/angularjs/59697?knId=641 随着需求的增加,项目的功能也渐渐复杂起来.这个时候,需要将项目模块化,将组件.指令和管道打包成内聚的功能块,正好可以探索一下Angular4中的子路由以及模块的懒加载. 之前在开发的时候,已经在根