Project

General

Profile

1
/**
2
 * @license
3
 * Copyright Google Inc. All Rights Reserved.
4
 *
5
 * Use of this source code is governed by an MIT-style license that can be
6
 * found in the LICENSE file at https://angular.io/license
7
 */
8
import { InjectionToken } from '@angular/core';
9
/**
10
 * A `ControlValueAccessor` acts as a bridge between the Angular forms API and a
11
 * native element in the DOM.
12
 *
13
 * Implement this interface if you want to create a custom form control directive
14
 * that integrates with Angular forms.
15
 *
16
 * @stable
17
 */
18
export interface ControlValueAccessor {
19
    /**
20
     * Writes a new value to the element.
21
     *
22
     * This method will be called by the forms API to write to the view when programmatic
23
     * (model -> view) changes are requested.
24
     *
25
     * Example implementation of `writeValue`:
26
     *
27
     * ```ts
28
     * writeValue(value: any): void {
29
     *   this._renderer.setProperty(this._elementRef.nativeElement, 'value', value);
30
     * }
31
     * ```
32
     */
33
    writeValue(obj: any): void;
34
    /**
35
     * Registers a callback function that should be called when the control's value
36
     * changes in the UI.
37
     *
38
     * This is called by the forms API on initialization so it can update the form
39
     * model when values propagate from the view (view -> model).
40
     *
41
     * If you are implementing `registerOnChange` in your own value accessor, you
42
     * will typically want to save the given function so your class can call it
43
     * at the appropriate time.
44
     *
45
     * ```ts
46
     * registerOnChange(fn: (_: any) => void): void {
47
     *   this._onChange = fn;
48
     * }
49
     * ```
50
     *
51
     * When the value changes in the UI, your class should call the registered
52
     * function to allow the forms API to update itself:
53
     *
54
     * ```ts
55
     * host: {
56
     *    (change): '_onChange($event.target.value)'
57
     * }
58
     * ```
59
     *
60
     */
61
    registerOnChange(fn: any): void;
62
    /**
63
     * Registers a callback function that should be called when the control receives
64
     * a blur event.
65
     *
66
     * This is called by the forms API on initialization so it can update the form model
67
     * on blur.
68
     *
69
     * If you are implementing `registerOnTouched` in your own value accessor, you
70
     * will typically want to save the given function so your class can call it
71
     * when the control should be considered blurred (a.k.a. "touched").
72
     *
73
     * ```ts
74
     * registerOnTouched(fn: any): void {
75
     *   this._onTouched = fn;
76
     * }
77
     * ```
78
     *
79
     * On blur (or equivalent), your class should call the registered function to allow
80
     * the forms API to update itself:
81
     *
82
     * ```ts
83
     * host: {
84
     *    '(blur)': '_onTouched()'
85
     * }
86
     * ```
87
     */
88
    registerOnTouched(fn: any): void;
89
    /**
90
     * This function is called by the forms API when the control status changes to
91
     * or from "DISABLED". Depending on the value, it should enable or disable the
92
     * appropriate DOM element.
93
     *
94
     * Example implementation of `setDisabledState`:
95
     *
96
     * ```ts
97
     * setDisabledState(isDisabled: boolean): void {
98
     *   this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
99
     * }
100
     * ```
101
     *
102
     * @param isDisabled
103
     */
104
    setDisabledState?(isDisabled: boolean): void;
105
}
106
/**
107
 * Used to provide a {@link ControlValueAccessor} for form controls.
108
 *
109
 * See {@link DefaultValueAccessor} for how to implement one.
110
 * @stable
111
 */
112
export declare const NG_VALUE_ACCESSOR: InjectionToken<ControlValueAccessor>;
(5-5/24)