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的更多相关文章

  1. [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 ...

  2. [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 ...

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

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

  4. [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 ...

  5. [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 ...

  6. uGUI练习(九) Toggle Button

    练习目标 练习单选框,多选框 Toggle Group:Toggle容器 Toggle:单一的选项 练习步骤 1.创建一个Panel,命名TogglePanel,添加Toggle Group组件,可以 ...

  7. [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 ...

  8. [React & Testing] Simulate Event testing

    Here we want to test a toggle button component, when the button was click, state should change, styl ...

  9. [Angular 2] ng-class and Encapsulated Component Styles

    import {Input, Component, View, NgClass} from "angular2/angular2"; @Component({ selector: ...

随机推荐

  1. HDU - 3594 Cactus

    这是一个有向仙人掌的题目,要求判定给定的图是不是强连通图,而且每一条边只能出现在一个环中,这里有一个介绍有向仙人掌的文档:http://files.cnblogs.com/ambition/cactu ...

  2. [codility]Array-closest-ascenders

    http://codility.com/demo/take-sample-test/pi2012 又是一道单调栈的题目.首先这道题目n^2是最朴素的做法.继续优化,因为和顺序有关就不好排序.然后,看到 ...

  3. [译]GotW #5:Overriding Virtual Functions

       虚函数是一个很基本的特性,但是它们偶尔会隐藏在很微妙的地方,然后等着你.如果你能回答下面的问题,那么你已经完全了解了它,你不太能浪费太多时间去调试类似下面的问题. Problem JG Ques ...

  4. 【HDOJ】1508 Alphacode

    简单DP.考虑10.20(出现0只能唯一组合).01(不成立). /* 1508 */ #include <iostream> #include <string> #inclu ...

  5. BZOJ3166: [Heoi2013]Alo

    3166: [Heoi2013]Alo Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 394  Solved: 204[Submit][Status] ...

  6. MongoDB中ObjectId的误区,以及引起的一系列问题

    近期对两个应用进行改造,在上线过程中出现一系列问题(其中一部分是由于ObjectId误区导致的) 先来了解下ObjectId: TimeStamp 前 4位是一个unix的时间戳,是一个int类别,我 ...

  7. 在 ASP.NET MVC 中创建自定义 HtmlHelper

    在ASP.NET MVC应用程序的开发中,我们常碰到类似Html.Label或Html.TextBox这样的代码,它将在网页上产生一个label或input标记.这些HtmlHelper的扩展方法有些 ...

  8. Java开发心得

    1. Spring概述 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性由Rod Johnson创建的.框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J ...

  9. [转]ASP.NET MVC 入门7、Hellper与数据的提交与绑定

    ASP.NET MVC提供了很多Hellper的方法,Hellper就是一些生成HTML代码的方法,方便我们书写HTML代码(有一部分的朋友更喜欢直接写HTML代码).我们也可以利用.NET 3.5的 ...

  10. 独立线程中实现QT GUI

    在网上搜集的资料: http://www.qtcentre.org/threads/16552-Starting-QT-GUI-in-a-seperate-Threadhttp://stackover ...