RadioControlValueAccessor
ControlValueAccessor 用于写入单选控件的值和监听单选控件值的更改。这个值访问器由 FormControlDirective、FormControlName 和 NgModel 指令使用。
The ControlValueAccessor for writing radio control values and listening to radio control changes. The value accessor is used by the FormControlDirective, FormControlName, and NgModel directives.
Exported from
选择器
input[type=radio][formControlName]input[type=radio][formControl]input[type=radio][ngModel]
属性
| 属性 | 说明 |
|---|---|
@Input() | 跟踪单选 input 元素的名称。 Tracks the name of the radio input element. |
@Input() | 跟踪绑定到指令的 Tracks the name of the |
@Input() | 跟踪单选 input 元素的值 Tracks the value of the radio input element |
说明
将单选按钮与响应式表单指令一起使用
Using radio buttons with reactive form directives
下面的示例演示了如何在响应式表单中使用单选按钮。当使用响应式表单的单选按钮时,同一组中的单选按钮应具有相同的 formControlName 。所提供的 name 属性是可选的。
The follow example shows how to use radio buttons in a reactive form. When using radio buttons in a reactive form, radio buttons in the same group should have the same formControlName. Providing a name attribute is optional.
import {Component} from '@angular/core';
import {FormControl, FormGroup} from '@angular/forms';
@Component({
selector: 'example-app',
template: `
<form [formGroup]="form">
<input type="radio" formControlName="food" value="beef" > Beef
<input type="radio" formControlName="food" value="lamb"> Lamb
<input type="radio" formControlName="food" value="fish"> Fish
</form>
<p>Form value: {{ form.value | json }}</p> <!-- {food: 'lamb' } -->
`,
})
export class ReactiveRadioButtonComp {
form = new FormGroup({
food: new FormControl('lamb'),
});
}
方法
在单选 input 元素上设置 “value”,并取消选中它。 Sets the "value" on the radio input element and unchecks it. |