[Angular 2] ng-class and Encapsulated Component Styles
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的更多相关文章
- Angular CLI ng常用指令整理
一.组件创建 ng generate component heroes 二.运行项目 ng serve --open //--open 立即打开 三.创建指令 ng g directive my-ne ...
- [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 ...
- [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 ...
- Angular: 执行ng lint后如何快速修改错误
当我第一次被分配到“修正执行ng lint语句后的错误”这项任务前,我就被导师提前告知这是一个很无聊的任务,当我开始后,我发现其实有一些办法可以加快这个无聊单调的工作.接下来,我就分享一下我的经验. ...
- 从flask视角理解angular(二)Blueprint VS Component
Component类似flask app下面的每个blueprint. import 'rxjs/add/operator/switchMap'; import { Component, OnInit ...
- [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 ...
- ANGULAR 使用 ng build --prod 编译报内存错误的解决办法
如果你遇到如下的情况 <--- Last few GCs ---> [13724:0000020D39C660D0] 231298 ms: Mark-sweep 1356.3 (1433. ...
- [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 ...
- [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 ...
随机推荐
- amf0解释一下
就简单记录一下省了以后忘了,amf0其实就几种数据格式的网络传输格式,比如数字,字符串,这些格式在传输的时候他给单独序列化了一下,主要支持以下这些: #define AMF0_NUMBER ((uin ...
- PHP 字符串常用方法
implode(“”,“”)-->字符串分割方法,第一个参数以什么样的形式分割,第二个参数需要分割的字符串 数组操作 is_array(),判断这个数是否是一个数组
- EasyUI篇のico
所有图标位置: /themes/icons css引用位置: /themes/icon.css 可自行添加16*16的小图片放在icons中,icon.css代码添加即可 例如: .icon-logo ...
- 知识库系统/知识管理系统 WCP
知识库系统/知识管理系统 WCP 本项目的应用场景是管理技术团队的相关知识(API.代码片段.知识定义.技术经验...) 但是其应用并不局限于这些应用,当然你最好下载一个安装版先试一试.其实这就是一个 ...
- 一份React-Native学习指南-感谢分享
自己在学习React-Native过程中整理的一份学习指南,包含 教程.开源app和资源网站等,还在不断更新中.欢迎pull requests! React-Native学习指南 本指南汇集React ...
- 转:misc_register、 register_chrdev 的区别总结
杂项设备(misc device) 杂项设备也是在嵌入式系统中用得比较多的一种设备驱动.在 Linux 内核的include/linux目录下有Miscdevice.h文件,要把自己定义的misc d ...
- bbc 大数据
http://video.sina.com.cn/v/b/107900125-2192582404.html
- BZOJ 1020 安全的航线flight
Description 在设计航线的时候,安全是一个很重要的问题.首先,最重要的是应采取一切措施确保飞行不会发生任何事故,但同时也需要做好最坏的打算,一旦事故发生,就要确保乘客有尽量高的生还几率.当飞 ...
- nginx 配置多个主机
<pre name="code" class="html"> server { listen 8001; server_name localhost ...
- 如果你不好好玩printf
昨天在跟Fiona讨论printf导致程序Crash的问题,就花了点时间看看究竟什么情况下会这样,有兴趣的童鞋可以看看:) 只要是玩过C或者C++的童鞋们,对printf肯定是再熟悉不过了.下面有几个 ...