Radio单选框

单选框

代码演示

基本用法

最简单的用法。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-radio-basic',
  4. template: `
  5. <div>
  6. <List [renderHeader]=(renderHeader)>
  7. <RadioItemGroup [(ngModel)]="selectedStatus1.value" (onChange)="onChange($event)">
  8. <RadioItem *ngFor="let i of data"
  9. [name]="i.name"
  10. [value]="i.value"
  11. >
  12. {{i.name}}
  13. </RadioItem>
  14. </RadioItemGroup>
  15. </List>
  16. <WhiteSpace [size]="'lg'"></WhiteSpace>
  17. <List>
  18. <RadioItemGroup [(ngModel)]="selectedStatus2.value" (onChange)="onChange2($event)">
  19. <RadioItem *ngFor="let i of data2"
  20. [name]="i.name"
  21. [value]="i.value"
  22. >
  23. {{i.name}}
  24. <Brief>{{i.extra}}</Brief>
  25. </RadioItem>
  26. </RadioItemGroup>
  27. </List>
  28. <List [renderHeader]=(renderHeader2)>
  29. <RadioItemGroup [(ngModel)]="selectedStatus1.value" (onChange)="onChange($event)">
  30. <RadioItem *ngFor="let i of data"
  31. [name]="i.name"
  32. [value]="i.value"
  33. [disabled]="true"
  34. >
  35. {{i.name}}
  36. </RadioItem>
  37. </RadioItemGroup>
  38. </List>
  39. <WhiteSpace [size]="'lg'"></WhiteSpace>
  40. <List>
  41. <RadioItemGroup [(ngModel)]="selectedStatus2.value" (onChange)="onChange2($event)">
  42. <RadioItem *ngFor="let i of data2"
  43. [name]="i.name"
  44. [value]="i.value"
  45. [disabled]="disabled"
  46. >
  47. {{i.name}}
  48. <Brief>{{i.extra}}</Brief>
  49. </RadioItem>
  50. </RadioItemGroup>
  51. </List>
  52. <Flex style="padding: 15px">
  53. <FlexItem style="padding: 15px 0; color: #888; flex: none">Radio demo(dustomized style)</FlexItem>
  54. <FlexItem>
  55. <label Radio
  56. class="my-radio"
  57. [name]="'Last Agree Item'"
  58. [value]="'Agree Submit'"
  59. (onChange)="onChange3($event)"
  60. >Agree</label>
  61. </FlexItem>
  62. </Flex>
  63. </div>
  64. `,
  65. styles: [
  66. `
  67. /deep/ .my-radio .am-radio {
  68. padding: 2.5px;
  69. border: 1px solid #ccc;
  70. border-radius: 50%;
  71. margin-right: 5px;
  72. box-sizing: initial;
  73. }
  74. `
  75. ]
  76. })
  77. export class DemoRadioBasicComponent {
  78. disabled: boolean = true;
  79. selectedStatus1 = {value: 0, name: 'doctor'};
  80. selectedStatus2 = { value: 0, name: 'basketball', extra: 'details' };
  81. data = [{ value: 0, name: 'doctor' }, { value: 1, name: 'bachelor' }];
  82. data2 = [{ value: 0, name: 'basketball', extra: 'details' }, { value: 1, name: 'football', extra: 'details' }];
  83. constructor() {}
  84. renderHeader() {
  85. return 'RadioItem demo';
  86. }
  87. renderHeader2() {
  88. return 'dd';
  89. }
  90. onChange = event => {
  91. console.log('ngmodel value: ', JSON.stringify(this.selectedStatus1));
  92. console.log('output radio status: ', JSON.stringify(event));
  93. }
  94. onChange2 = event => {
  95. console.log('ngmodel value: ', JSON.stringify(this.selectedStatus2));
  96. console.log('output radio status: ', JSON.stringify(event));
  97. }
  98. onChange3 = e => {
  99. this.disabled = false;
  100. console.log('agree submit', e);
  101. }
  102. }

Radio 单选框 - 图1

API

Radio

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

RadioItemGroup

单选框组合,包裹一组RadioItems。

属性说明类型默认值
ngModel指定选中的RadioItem对应的value, 可双向绑定string / number
onChangeRadioItem选中变化时的回调EventEmitter<Object: {name, value}>

RadioItem

基于ListItemRadio进行封装,ListItemextra属性固定传入Radio,其他属性和ListItem一致(除了onClick回调事件不可用之外,因为在这里是由RadioItemGroup传递回调事件)。

属性说明类型默认值
namenameString
valuevalueString
disabled禁用Booleanfalse