[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 transclusion in Angular 2, setting up your own two-way binding, and making the button into a reusable component.
toggle-button.ts:
- import {Component, Input, Output, EventEmitter} from '@angular/core';
- @Component({
- selector: 'toggle-button',
- styles: [
- `
- .on{
- background-color: white;
- color: black;
- }
- .off{
- background-color: black;
- color: white;
- }
- `
- ],
- template: `
- <button
- (click)="onClick($event)"
- [ngClass]="on ? 'on' : 'off'">
- <ng-content></ng-content>
- </button>
- `
- })
- export class ToggleButton{
- @Input() on;
- @Output() onChange = new EventEmitter();
- onClick($event){
- this.on = !this.on;
- this.onChange.emit(this.on);
- }
- }
app.ts:
- import {Component} from '@angular/core';
- import {Observable} from 'rxjs/Observable';
- import {ToggleButton} from './components/toggle-button/toggle-button';
- export interface AppState{
- todos: Todo[];
- }
- @Component({
- moduleId: module.id,
- selector: 'seed-app',
- providers: [],
- pipes: [],
- directives: [ToggleButton],
- template: `
- <toggle-button [(on)]="state">
- {{state ? 'On' : 'Off'}}
- </toggle-button>
- `,
- })
- export class SeedApp {
- state = false;
- }
[Angular 2] Building a Toggle Button Component的更多相关文章
- [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 2] @ViewChild to access Child component's method
When you want to access child component's method, you can use @ViewChild in the parent: Parent Compo ...
- 从flask视角理解angular(二)Blueprint VS Component
Component类似flask app下面的每个blueprint. import 'rxjs/add/operator/switchMap'; import { Component, OnInit ...
- [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 ...
- uGUI练习(九) Toggle Button
练习目标 练习单选框,多选框 Toggle Group:Toggle容器 Toggle:单一的选项 练习步骤 1.创建一个Panel,命名TogglePanel,添加Toggle Group组件,可以 ...
- [React] Styling a React button component with Radium
React's inline styles allow components to stand on their own by not requiring any external CSS. Howe ...
- [React & Testing] Simulate Event testing
Here we want to test a toggle button component, when the button was click, state should change, styl ...
- [Angular 2] ng-class and Encapsulated Component Styles
import {Input, Component, View, NgClass} from "angular2/angular2"; @Component({ selector: ...
随机推荐
- cocos-html5 Json 灵活 遍历方式 不同方式的缺陷,优点总结
1,四种解析Json的方式:Part 1 var list1 = [1,3,4]; alert(list1[1]); var list2 = [{"name":"leam ...
- Ubuntu下APACHE HTTPS安装和配置
http://blog.csdn.net/newjueqi/article/details/9789659
- 几篇很有用的USB开发资料
1,USB描述符详解:http://blog.csdn.net/alien75/article/details/4622319 2,一种嵌入式USBMiniHost系统设计与实现:http://www ...
- USACO3.25Magic Squares(bfs)
/* ID: shangca2 LANG: C++ TASK: msquare */ #include <iostream> #include<cstdio> #include ...
- android Service Activity三种交互方式(付源码)
android SDK提供了Service,用于类似Linix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service):用于应用程序内部 远程服务(Rem ...
- 关于I/O的那点事
转载请著名作者和地址http://www.cnblogs.com/scotth/p/3645489.html 1.关于 IO (fopen出现的错误 errorCode 183) 相关知识点: < ...
- BZOJ2005: [Noi2010]能量采集 莫比乌斯反演的另一种方法——nlogn筛
分析:http://www.cnblogs.com/huhuuu/archive/2011/11/25/2263803.html 注:从这个题收获了两点 1,第一象限(x,y)到(0,0)的线段上整点 ...
- javascript二维数组
var a= new Array(new Array(1,2),new Array('b','c')); document.write(a[1][1]); 说白了,就是利用for循环定义二维数组! & ...
- python __enter__ 与 __exit__的作用,以及与 with 语句的关系
转载自:http://linbo.github.io/2013/01/08/python-with/ (一直不知道博客园哪里发转载文章) With语句是什么? 有一些任务,可能事先需要设置,事后做清理 ...
- mac os develop
. 安装PCRE Download latest PCRE. After download go to download directory from terminal. $ cd ~/Downloa ...