ControlValueAccessor
定义一个接口,该接口充当 Angular 表单 API 和 DOM 中的原生元素之间的桥梁。
Defines an interface that acts as a bridge between the Angular forms API and a native element in the DOM.
interface ControlValueAccessor {
writeValue(obj: any): void
registerOnChange(fn: any): void
registerOnTouched(fn: any): void
setDisabledState(isDisabled: boolean)?: void
}
参见
DefaultValueAccessor
说明
实现此接口以创建与 Angular 表单集成的自定义表单控件指令。
Implement this interface to create a custom form control directive that integrates with Angular forms.
方法
将新值写入元素。 Writes a new value to the element. |
当请求从模型到视图的编程更改时,表单 API 会调用此方法以写入视图。 This method is called by the forms API to write to the view when programmatic changes from model to view are requested. |
使用说明向元素写入值Write a value to the element以下示例将一个值写入原生 DOM 元素。 The following example writes a value to the native DOM element.
|
注册一个回调函数,该控件的值在 UI 中更改时将调用该回调函数。 Registers a callback function that is called when the control's value changes in the UI. |
当值从视图传播到模型时,表单 API 会在初始化时调用此方法以更新表单模型。 This method is called by the forms API on initialization to update the form model when values propagate from the view to the model. 在你自己的值访问器中实现 When implementing the |
使用说明存储变更函数Store the change function以下示例将所提供的函数存储为内部方法。 The following example stores the provided function as an internal method.
当用户界面中的值更改时,请调用已注册的函数以允许表单 API 自行更新: When the value changes in the UI, call the registered function to allow the forms API to update itself:
|
注册一个在初始化时由表单 API 调用的回调函数,以在失焦时更新表单模型。 Registers a callback function that is called by the forms API on initialization to update the form model on blur. |
在你自己的值访问器中实现 When implementing |
使用说明存储回调函数Store the callback function以下示例将所提供的函数存储为内部方法。 The following example stores the provided function as an internal method.
在 blur(或等效事件)时,你的类应调用已注册的函数以允许表单 API 自行更新: On blur (or equivalent), your class should call the registered function to allow the forms API to update itself:
|
当控件状态更改为 “DISABLED” 或从 “DISABLED” 更改时,表单 API 要调用的函数。根据其状态,它会启用或禁用适当的 DOM 元素。 Function that is called by the forms API when the control status changes to or from 'DISABLED'. Depending on the status, it enables or disables the appropriate DOM element. |
使用说明以下是将 disabled 属性写入原生 DOM 元素的示例: The following is an example of writing the disabled property to a native DOM element:
|