Checkbox复选框

复选框

代码演示

基本用法

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-checkbox-basic',
  4. template: `
  5. <List [renderHeader]="renderHeader">
  6. <CheckboxItem *ngFor="let i of checkItemListData"
  7. [name]="i.name"
  8. [value]="i.value"
  9. [(ngModel)]="i.checked"
  10. (onChange)="onChange($event)"
  11. >
  12. {{ i.name }}
  13. </CheckboxItem>
  14. <CheckboxItem multipleLine
  15. key="disabled"
  16. data-seed="logId"
  17. [disabled]="disabledStatus"
  18. [(ngModel)]="disabledCheckboxItemStatus"
  19. >
  20. Undergraduate<Brief>Auxiliary text</Brief>
  21. </CheckboxItem>
  22. </List>
  23. <Flex>
  24. <FlexItem>
  25. <AgreeItem data-seed="logId"
  26. [name]="agreeItemData.name"
  27. [value]="agreeItemData.value"
  28. [(ngModel)]="agreeItemData.checked"
  29. (onChange)="onChange2($event)"
  30. >
  31. Agree <a (click)="onClick($event)">agreement</a>
  32. </AgreeItem>
  33. </FlexItem>
  34. </Flex>
  35. `
  36. })
  37. export class DemoCheckboxBasicComponent {
  38. checkItemListData = [
  39. { value: 0, name: 'Ph.D.', checked: false },
  40. { value: 1, name: 'Bachelor', checked: true },
  41. { value: 2, name: 'College diploma', checked: false }
  42. ];
  43. disabledStatus: boolean = true;
  44. disabledCheckboxItemStatus: boolean = true;
  45. agreeItemData = { value: 'Agree Submit', name: 'Agree Item', checked: true };
  46. onChange = (val: any) => {
  47. console.log('onChange Event: ', val);
  48. console.log('changed data: ', this.checkItemListData);
  49. }
  50. onChange2 = e => {
  51. this.disabledStatus = !this.disabledStatus;
  52. console.log('onChange2 Event: ', e);
  53. console.log('agreeItemData: ', this.agreeItemData);
  54. }
  55. onClick(e) {
  56. e.stopPropagation();
  57. e.preventDefault();
  58. alert('agree it');
  59. }
  60. renderHeader() {
  61. return 'CheckboxItem demo';
  62. }
  63. }

Checkbox 复选框 - 图1

API

Checkbox

属性说明类型默认值
namenameString-
valuevalueString-
checked指定当前是否选中Boolean
disabled是否禁用Booleanfalse
onChangechange 事件触发的回调函数(name: string, value: string, checked: boolean) => void

CheckboxItem

基于ListItemCheckbox进行封装,ListItemthumb属性固定传入Checkbox,其他属性和ListItem一致(除了onClick回调事件,在这里它被onChange回调事件所替代)。

属性说明类型默认值
namenameString-
valuevalueString-
ngModel指定当前是否选中,可双向绑定Booleanfalse
disabled是否禁用Booleanfalse
onChangechange 事件触发的回调函数(name: string, value: string, checked: boolean) => void

AgreeItem

用于同意协议这种场景的复选框

属性说明类型默认值
namenameString-
valuevalueString-
ngModel指定当前是否选中,可双向绑定Booleanfalse
disabled是否禁用Booleanfalse
onChangechange 事件触发的回调函数(name: string, value: string, checked: boolean) => void