import {Input, Component, View, NgClass} from "angular2/angular2";

@Component({
selector: 'todo-item-render'
})
@View({
directives: [NgClass],
styles: [`
.started{
color: green;
} .completed {
text-decoration: line-through;
}
`],
template: `
<div>
<span [ng-class]="todoinput.status">{{todoinput.title}}</span>
<button (click)="todoinput.toggle()">Toggle</button>
</div>
`
}) export class TodoItemRender{
@Input() todoinput: TodoModel;
}

Many Components require different styles based on a set of conditions. Angular 2 helps you style your Components by allows you to define Styles inline, then choosing which styles to use based on the current values in your Controller.

You can define a static var on the TodoModel:

export class TodoModel{
static STARTED: string = "started";
static COMPLETED: string = "completed";
status: string = TodoModel.STARTED;
constructor(
public title: string = ""
){} toggle(): void{
if(this.status === TodoModel.STARTED) this.status = TodoModel.COMPLETED;
else this.status = TodoModel.STARTED;
}
} export class TodoService{
todos: TodoModel[] = [
new TodoModel('eat'),
new TodoModel('sleep'),
new TodoModel('work')
]; addTodo(value: TodoModel):void {
this.todos.push(value);
}
}

Then in the todoItemRender, you can require TodoModel and use the static var:

import {Input, Component, View, NgClass} from "angular2/angular2";
import {TodoModel} from './todoService'; @Component({
selector: 'todo-item-render'
})
@View({
directives: [NgClass],
styles: [`
.${TodoModel.STARTED}{
color: green;
} .${TodoModel.COMPLETED}{
text-decoration: line-through;
}
`],
template: `
<div>
<span [ng-class]="todoinput.status">{{todoinput.title}}</span>
<button (click)="todoinput.toggle()">Toggle</button>
</div>
`
}) export class TodoItemRender{
@Input() todoinput: TodoModel;
}

[Angular 2] ng-class and Encapsulated Component Styles的更多相关文章

  1. Angular CLI ng常用指令整理

    一.组件创建 ng generate component heroes 二.运行项目 ng serve --open //--open 立即打开 三.创建指令 ng g directive my-ne ...

  2. [Angular 2]ng-class and Encapsulated Component Style2

    Many Components require different styles based on a set of conditions. Angular 2 helps you style you ...

  3. [Angular 2] Building a Toggle Button Component

    This lesson shows you how to build a Toggle Button in Angular 2 from scratch. It covers using transc ...

  4. Angular: 执行ng lint后如何快速修改错误

    当我第一次被分配到“修正执行ng lint语句后的错误”这项任务前,我就被导师提前告知这是一个很无聊的任务,当我开始后,我发现其实有一些办法可以加快这个无聊单调的工作.接下来,我就分享一下我的经验. ...

  5. 从flask视角理解angular(二)Blueprint VS Component

    Component类似flask app下面的每个blueprint. import 'rxjs/add/operator/switchMap'; import { Component, OnInit ...

  6. [Angular 2] Generate and Render Angular 2 Template Elements in a Component

    Angular 2 Components have templates, but you can also create templates inside of your templates usin ...

  7. ANGULAR 使用 ng build --prod 编译报内存错误的解决办法

    如果你遇到如下的情况 <--- Last few GCs ---> [13724:0000020D39C660D0] 231298 ms: Mark-sweep 1356.3 (1433. ...

  8. [Angular & Unit Testing] Testing a RouterOutlet component

    The way to test router componet needs a little bit setup, first we need to create a "router-stu ...

  9. [Angular Unit Testing] Debug unit testing -- component rendering

    If sometime you want to log out the comonent html to see whether the html render correctly, you can ...

随机推荐

  1. jquery中eq和get的区别与使用方法

    $("p").eq(0).css("color") //因为eq(num)返回的是个jq对象,所以可以用jq的方法css使用get来获得第一个p标签的color ...

  2. 每个Linux新手都应该记住的10个基本Linux命令

    Linux对我们的生活有着很大的影响.至少,你的安卓手机上面就有Linux内核.然而,头一次入手Linux只会让你觉得不适.因为在Linux上,你通常应该使用终端命令,而不是只要点击启动器图像(就像你 ...

  3. SQL 测试

    1.SQL 指的是? 您的回答:Structured Query Language 2.哪个 SQL 语句用于从数据库中提取数据? 您的回答:SELECT 3.哪条 SQL 语句用于更新数据库中的数据 ...

  4. nav

    $(document).ready(function() { $(window).resize(function(){ var need=0; var ul_max_width = $(window) ...

  5. TatukGIS - GisDefs - ColorToHSL 过程

    过程名称  ColorToHSL 所在单元  GisDefs 过程原型           procedure ColorToHSL(const _color: TColor; var _h: Rea ...

  6. Python3.X与urllib

    在Python3.X中使用urllib时,不能像Python2.X一样直接使用: import urllib response = urllib.urlopen("http://www.ba ...

  7. 跨平台的CStdString类,实现了CString的接口

    在实际工作中,std的string功能相对于MFC的CString来说,实在是相形见绌. CStdString类实现了CString的功能,支持跨平台. // ==================== ...

  8. asp.net web api long running task

    http://stackoverflow.com/questions/17577016/long-running-task-in-webapi http://blog.stephencleary.co ...

  9. Dungeons and Candies

    Zepto Code Rush 2014:http://codeforces.com/problemset/problem/436/C 题意:k个点,每个点都是一个n * m的char型矩阵.对与每个 ...

  10. CP30,DBCP数据源配置

    Spring中 CP30数据源配置 <!-- 加载属性文件 01--> <bean id= "propertyConfigurer" class="or ...