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:

  1. import {Component, Input, Output, EventEmitter} from '@angular/core';
  2. @Component({
  3. selector: 'toggle-button',
  4. styles: [
  5. `
  6. .on{
  7. background-color: white;
  8. color: black;
  9. }
  10.  
  11. .off{
  12. background-color: black;
  13. color: white;
  14. }
  15. `
  16. ],
  17. template: `
  18. <button
  19. (click)="onClick($event)"
  20. [ngClass]="on ? 'on' : 'off'">
  21. <ng-content></ng-content>
  22. </button>
  23. `
  24. })
  25. export class ToggleButton{
  26. @Input() on;
  27. @Output() onChange = new EventEmitter();
  28. onClick($event){
  29. this.on = !this.on;
  30. this.onChange.emit(this.on);
  31. }
  32. }

app.ts:

  1. import {Component} from '@angular/core';
  2. import {Observable} from 'rxjs/Observable';
  3. import {ToggleButton} from './components/toggle-button/toggle-button';
  4.  
  5. export interface AppState{
  6. todos: Todo[];
  7. }
  8. @Component({
  9. moduleId: module.id,
  10. selector: 'seed-app',
  11. providers: [],
  12. pipes: [],
  13. directives: [ToggleButton],
  14. template: `
  15. <toggle-button [(on)]="state">
  16. {{state ? 'On' : 'Off'}}
  17. </toggle-button>
  18. `,
  19. })
  20. export class SeedApp {
  21. state = false;
  22. }

[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. cocos-html5 Json 灵活 遍历方式 不同方式的缺陷,优点总结

    1,四种解析Json的方式:Part 1 var list1 = [1,3,4]; alert(list1[1]); var list2 = [{"name":"leam ...

  2. Ubuntu下APACHE HTTPS安装和配置

    http://blog.csdn.net/newjueqi/article/details/9789659

  3. 几篇很有用的USB开发资料

    1,USB描述符详解:http://blog.csdn.net/alien75/article/details/4622319 2,一种嵌入式USBMiniHost系统设计与实现:http://www ...

  4. USACO3.25Magic Squares(bfs)

    /* ID: shangca2 LANG: C++ TASK: msquare */ #include <iostream> #include<cstdio> #include ...

  5. android Service Activity三种交互方式(付源码)

    android SDK提供了Service,用于类似Linix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service):用于应用程序内部 远程服务(Rem ...

  6. 关于I/O的那点事

    转载请著名作者和地址http://www.cnblogs.com/scotth/p/3645489.html 1.关于 IO (fopen出现的错误 errorCode 183) 相关知识点: < ...

  7. BZOJ2005: [Noi2010]能量采集 莫比乌斯反演的另一种方法——nlogn筛

    分析:http://www.cnblogs.com/huhuuu/archive/2011/11/25/2263803.html 注:从这个题收获了两点 1,第一象限(x,y)到(0,0)的线段上整点 ...

  8. javascript二维数组

    var a= new Array(new Array(1,2),new Array('b','c')); document.write(a[1][1]); 说白了,就是利用for循环定义二维数组! & ...

  9. python __enter__ 与 __exit__的作用,以及与 with 语句的关系

    转载自:http://linbo.github.io/2013/01/08/python-with/ (一直不知道博客园哪里发转载文章) With语句是什么? 有一些任务,可能事先需要设置,事后做清理 ...

  10. mac os develop

    . 安装PCRE Download latest PCRE. After download go to download directory from terminal. $ cd ~/Downloa ...