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. golang bufio writer,reader 缓存规则

    读,写,缓冲区可以杜绝频繁的读,写动作1.写缓存,如果一次write的长度大于buffer长度那么久发送当前缓冲区的内容并且发送要写入的内容,就是不在缓存了.如果发送的内容小于buffer长度,就按缓 ...

  2. ajax 文件上传,ajax

    ajax 文件上传,ajax 啥也不说了,直接上代码! <input type="file" id="file" name="myfile&qu ...

  3. Python学习 - 使用BeautifulSoup来解析网页一:基础入门

    写技术博客主要就是总结和交流的,如果文章用错,请指正啊! 以前一直在使用SGMLParser,这个太费时间和精力了,现在为了毕业设计,改用BeautifulSoup来实现HTML页面的解析工作的. 一 ...

  4. vim技巧:折叠快捷键

    vim技巧:折叠快捷键 以前用的挺熟的,一段时间不用了,快捷键又忘了,不得不重新再看手册,今天专门整理一下,以后查找起来也比较方便. zc 折叠,只折叠最外层的折叠zC 对所在范围内所有嵌套的折叠点进 ...

  5. C++不能中断构造函数来拒绝产生对象(在构造和析构中抛出异常)

    这是我的感觉,具体需要研究一下- 找到一篇文章:在构造和析构中抛出异常 测试验证在类构造和析构中抛出异常, 是否会调用该类析构. 如果在一个类成员函数中抛异常, 可以进入该类的析构函数. /// @f ...

  6. Eclipse调试的一些小技巧

    不要使用System.out.println作为调试工具 启用所有组件的详细的日志记录级别 使用一个日志分析器来阅读日志 1.条件断点 想象一下我们平时如何添加断点,通常的做法是双击行号的左边.在de ...

  7. rsync命令来实现将服务器端的文件备份到客户端

    rsync命令来实现将服务器端的文件备份到客户端

  8. OpenSSH远程拒绝服务漏洞

    漏洞版本: OpenSSH 漏洞描述: Bugtraq ID:61286 OpenSSH是一种开放源码的SSH协议的实现 OpenSSH存在一个安全漏洞,允许远程攻击者利用漏洞提交恶意请求,使应用程序 ...

  9. joelonsoftware 读书摘录

    joelonsoftware 读书摘录   <五个为什么>  1.“黑天鹅难题”,代表外来因素,是一个超出正常预料之外的事件.  2.丰田佐吉的“五个为什么”,当某个地方出现问题时,你就一 ...

  10. github继续折腾

    又在折腾github了,本来以前在neworiginou这个github上上传过项目的: 现在想在另一个github上joely上传项目,发现按以前的流程做个测试,居然没能上传成功! 经发现,以前的n ...