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. Java中堆和栈创建对象的区别

    http://blog.csdn.net/hbhhww/article/details/8152838

  2. 关于 ASP.NET MVC 4 如果管理用户

    很久没上来写博客,因为自己没写博客的日子里去学了一下OBJECTIVE-C 和 ASP.NET MVC.最近在学ASP.NET MVC 4,有个问题一直在困扰着我,就是怎样管理用SIMPLE MEMB ...

  3. SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-004-Pizza例子的用户流程(flowExecutionKey、_eventId_phoneEntered、flowExecutionUrl )

    一. 1. 2. 3.customer-flow.xml 自己定义customer,最后output <?xml version="1.0" encoding="U ...

  4. C语言考试解答十题

    学院比较奇葩,大一下期让学的VB,这学期就要学C++了,然后在开学的前三个周没有课,就由老师讲三个周的C语言,每天9:30~11:30听课,除去放假和双休日,实际听课时间一共是12天*2小时,下午是1 ...

  5. 栈和队列的面试题Java

    栈和队列: 面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min ...

  6. git获取远端版本库上的Tag (没有clone[远端的版本库太大了])

    方法一 http://stackoverflow.com/questions/25815202/git-fetch-a-single-commit The git fetch command deli ...

  7. VisualC#数据库高级教程文档分享

    这一节我们演示下怎样使用VS2010创建与发布MVC3建立的网站.使用VS2010创建MVC3.0网站,需要下载MVC3.0的安装包,这个大家可以去网络上下载.     1.项目创建         ...

  8. arch Linux not found device 错误解决

    使用Archlinux LiveCD mount /dev.sda1 /mnt (有boot分区的挂boot) Running mkinitcpio -p linux Running grub-mkc ...

  9. values of type NSInteger should not be used as format arguments; 关于Xcode中烦人的32位与64位警告处理方法.

    http://stackoverflow.com/questions/16075559/why-does-an-nsinteger-variable-have-to-be-casted-to-long ...

  10. apache配置虚拟主机的三种方式

    Apache 配置虚拟主机三种方式   一.基于IP 1. 假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP: [root@localhos ...