DefaultValueAccessor
The default ControlValueAccessor
for writing a value and listening to changes on input elements. The accessor is used by the FormControlDirective
, FormControlName
, and NgModel
directives.
Exported from
选择器
input:not([type=checkbox])[formControlName]
textarea[formControlName]
input:not([type=checkbox])[formControl]
textarea[formControl]
input:not([type=checkbox])[ngModel]
textarea[ngModel]
[ngDefaultControl]
说明
使用默认值访问器
Using the default value accessor
以下示例演示了如何使用输入元素激活默认值访问器(在这种情况下为文本字段)。
The following example shows how to use an input element that activates the default value accessor (in this case, a text field).
const firstNameControl = new FormControl();
<input type="text" [formControl]="firstNameControl">
默认情况下,此值访问器用于 <input type="text">
和 <textarea>
元素,但你也可以将其用于具有类似行为且不需要特殊处理的自定义组件。为了将默认值访问器附加到自定义元素,请添加 ngDefaultControl
属性,如下所示。
This value accessor is used by default for <input type="text">
and <textarea>
elements, but you could also use it for custom components that have similar behavior and do not require special processing. In order to attach the default value accessor to a custom element, add the ngDefaultControl
attribute as shown below.
<custom-input-component ngDefaultControl [(ngModel)]="value"></custom-input-component>