[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: ...
随机推荐
- ANDROID_MARS学习笔记_S01原始版_014_WIFI
一.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
- Android USB Host 与 Hid 设备通信bulkTransfer()返回-1问题的原因
近期一直在做Android USB Host 与USB Hid设备(STM32FXXX)的通信,遇到了很多问题.项目源码以及所遇到的其他问题可以见本博客其他相关文章,这里重点讲一下bulkTransf ...
- ruby quiz The Solitaire Cipher
solitaire cipher:http://en.wikipedia.org/wiki/Solitaire_(cipher) https://www.schneier.com/solitaire. ...
- poj1691(dfs)
链接 dfs了 写得有点乱 #include <iostream> #include<cstdio> #include<cstring> #include<a ...
- 常用的Web服务器
常用的Web服务器有IIS.Apache.Tomcat.Jboss.Resin.Weblogic.WebSpher IISIIS服务是Windows产品自带的一种免费的Web服务器,安装配置简单,主要 ...
- ☀【组件】字符串 string
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- 【转】OS X Mavericks: 防止 Mac 进入睡眠 -- 不错
原文网址:https://support.apple.com/kb/PH13808?locale=zh_CN&viewlocale=zh_CN 某些 Mac 电脑将在不活跃一段时间后自动进入睡 ...
- 注意!ASP.NET MVC 3 的一个 OutputCache 问题
在用 ASP.NET MVC 3 重写博客园网站首页时,特地留意了一下这个缓存问题,通过这篇博文分享一下. 在 ASP.NET MVC 3 中如果使用了 OutputCache,一定要在 Action ...
- 从零开始学习jQuery (十) jQueryUI常用功能实战
一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案, 即使你会使用jQuery也能在阅读中发现些许秘籍. 本文是实战篇. 使用jQueryUI完成制作网站 ...
- HDU4725 The Shortest Path in Nya Graph dij
分析:对于每一层,原来n个点,然后扩展为原来的三倍,每一层扩展一个入点,一个出点,然后跑最短路 注:tmd我把一个n写成m了,然后wa了7次,我都要怀疑人生了 #include<cstdio&g ...