Switch滑动开关

在两个互斥对象进行选择,eg:选择开或关。

规则

  • 只在 List 中使用。
  • 避免增加额外的文案来描述当前 Switch 的值。

代码演示

基本用法

最简单的用法。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-switch-basic',
  4. template: `
  5. <WingBlank>
  6. <List [className]="'my-list'" [renderHeader]=(renderHeader)>
  7. <ListItem multipleLine [extra]="on">
  8. On
  9. </ListItem>
  10. <ListItem multipleLine [extra]="off">
  11. Off
  12. </ListItem>
  13. <ListItem multipleLine [extra]="disableOff">
  14. Disable Off
  15. </ListItem>
  16. <ListItem multipleLine [extra]="disableOn">
  17. Disable On
  18. </ListItem>
  19. <ListItem multipleLine [extra]="android">
  20. Style for Android
  21. </ListItem>
  22. <ListItem multipleLine [extra]="iOS">
  23. Style for iOS
  24. </ListItem>
  25. <ListItem multipleLine [extra]="colorAndroid">
  26. Color for Android
  27. </ListItem>
  28. <ListItem multipleLine [extra]="coloriOS">
  29. Color for iOS
  30. </ListItem>
  31. </List>
  32. <ng-template #on>
  33. <Switch [checked]="true" (onChange)="check($event)" (onClick)="onClick($event)"></Switch>
  34. </ng-template>
  35. <ng-template #off>
  36. <Switch [checked]="false" (onChange)="check($event)" (onClick)="onClick($event)"></Switch>
  37. </ng-template>
  38. <ng-template #disableOff>
  39. <Switch [checked]="false" [disabled]="true" (onChange)="check($event)" (onClick)="onClick($event)"></Switch>
  40. </ng-template>
  41. <ng-template #disableOn>
  42. <Switch [checked]="true" [disabled]="true" (onChange)="check($event)" (onClick)="onClick($event)"></Switch>
  43. </ng-template>
  44. <ng-template #android>
  45. <Switch [checked]="checked" [platform]="'android'" (onChange)="check($event)" (onClick)="onClick($event)"></Switch>
  46. </ng-template>
  47. <ng-template #iOS>
  48. <Switch [(ngModel)]="checked" [platform]="'ios'" (onChange)="check($event)"></Switch>
  49. </ng-template>
  50. <ng-template #colorAndroid>
  51. <Switch [checked]="checked" [platform]="'android'" [color]="'#ff0000'" (onChange)="check($event)" (onClick)="onClick($event)"></Switch>
  52. </ng-template>
  53. <ng-template #coloriOS>
  54. <Switch [checked]="checked" [platform]="'ios'" [color]="'#ff0000'" (onChange)="check($event)"></Switch>
  55. </ng-template>
  56. </WingBlank>
  57. `
  58. })
  59. export class DemoSwitchBasicComponent {
  60. checked = true;
  61. constructor() {}
  62. check(event) {
  63. console.log(event);
  64. }
  65. onClick(event) {
  66. console.log(event);
  67. }
  68. renderHeader() {
  69. return 'Form switch';
  70. }
  71. }

Switch 滑动开关 - 图1

API

属性说明类型默认值
ngModel当前值,可双向绑定Booleanfalse
checked是否默认选中Booleanfalse
disabled是否不可修改Booleanfalse
onChangechange 事件触发的回调函数(checked: bool): void
color开关打开后的颜色String#4dd865
nameswitch 的 nameString
platform设定组件的平台特有样式, 可选值为 android, ios, 默认为 iosString'ios'
onClickclick事件触发的回调函数,当switch为disabled时,入参的值始终是默认传入的checked值。(checked: bool): void