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
  46. [checked]="checked"
  47. [platform]="'android'"
  48. (onChange)="check($event)"
  49. (onClick)="onClick($event)"
  50. ></Switch>
  51. </ng-template>
  52. <ng-template #iOS>
  53. <Switch [(ngModel)]="checked" [platform]="'ios'" (onChange)="check($event)"></Switch>
  54. </ng-template>
  55. <ng-template #colorAndroid>
  56. <Switch
  57. [checked]="checked"
  58. [platform]="'android'"
  59. [color]="'#ff0000'"
  60. (onChange)="check($event)"
  61. (onClick)="onClick($event)"
  62. ></Switch>
  63. </ng-template>
  64. <ng-template #coloriOS>
  65. <Switch [checked]="checked" [platform]="'ios'" [color]="'#ff0000'" (onChange)="check($event)"></Switch>
  66. </ng-template>
  67. </WingBlank>
  68. `
  69. })
  70. export class DemoSwitchBasicComponent {
  71. checked = true;
  72. constructor() {}
  73. check(event) {
  74. console.log(event);
  75. }
  76. onClick(event) {
  77. console.log(event);
  78. }
  79. renderHeader() {
  80. return 'Form switch';
  81. }
  82. }

Switch滑动开关 - 图1

API

参数说明类型默认值
[checked]是否默认选中booleanfalse
[disabled]是否不可修改booleanfalse
[color]开关打开后的颜色string‘#4dd865’
[name]switch 的 namestring-
[platform]设定组件的平台特有样式‘ios’ | ‘android’‘ios’
[(ngModel)]当前值booleanfalse
(onChange)change 事件触发的回调函数EventEmitter<boolean>-
(onClick)click事件触发的回调函数,当switch为disabled时,入参的值始终是默认传入的checked值EventEmitter<boolean>-