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. eclipse+tomcat7解决项目中文乱码的一个思路

    1. 在代码层面进行编码的修改操作,参考博文的方法一:http://www.cnblogs.com/longshiyVip/p/4873058.html 2. 如果项目使用了struts2等前端框架, ...

  2. [wikioi]合并果子

    http://wikioi.com/problem/1063/ 这题是贪心+堆.主要想练习一下堆的写法.算法导论里的方法名是heapify()等,但大家经常用更直观的down(), up()方法(向上 ...

  3. OpenSSL 与 SSL 数字证书概念贴

    SSL/TLS 介绍见文章 SSL/TLS原理详解(http://seanlook.com/2015/01/07/tls-ssl). 如果你想快速自建CA然后签发数字证书,请移步 基于OpenSSL自 ...

  4. spring-boot启动debug信息中non-fatal error解决

    java.lang.ClassNotFoundException: org.springframework.data.web.config.EnableSpringDataWebSupport添加依赖 ...

  5. Android-锁屏功能

    当屏幕多久没有点击的时候,进行某种操作就是所谓的锁屏功能. onCreate: public void addRunnable() { handler.postDelayed(runnable, Co ...

  6. JAVA线程优化

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  7. JAVA bean与XML互转的利器---XStream

    最近在项目中遇到了JAVA bean 和XML互转的需求, 本来准备循规蹈矩使用dom4j忽然想起来之前曾接触过的XStream, 一番研究豁然开朗,利器啊利器, 下来就XStream的一些用法与大家 ...

  8. 【DataStructure In Python】Python实现各种排序算法

    使用Python实现直接插入排序.希尔排序.简单选择排序.冒泡排序.快速排序.归并排序.基数排序. #! /usr/bin/env python # DataStructure Sort # Inse ...

  9. Form Personalization应用总结

    1 Form Personalization 简介 Oracle EBS 11.5.10增加了Form Personalization功能,该功能不仅是技术功能的一次增强,也是对业务功能的扩展,提高了 ...

  10. Android开发之PackageManager类

    PackageManger,可以获取到手机上所有的App,并可以获取到每个App中清单文件的所有内容. 设置应用程序版本号在应用程序的manifest文件中定义应用程序版本信息.2个必须同时定义的属性 ...