This article mainly introduces the relevant information of Angular19 custom form controls. It is very good and has reference value. Friends in need can refer to it
1 Requirements
When developers need a specific form control, they need to develop a control similar to the default provided form control as a form control; custom form controls must consider the relationship between the model and the view How to interact with data
2 Official Document-> Click to go
Angular The ControlValueAccessor interface is provided for developers to assist developers in building custom form controls. Developers only need to implement the methods in the ControlValueAccessor interface in the custom form control class to achieve data interaction between the model and the view
1 2 3 4 5 6 |
|
2.1 writeValue
1 |
|
This method is used to write values to elements in custom form controls;
This parameter value (obj) uses this The components of the custom form control are passed through the data binding of the template form or responsive form;
In the class of the custom form control, you only need to assign this value (obj) to a member variable. , the view of the custom form control will display this value through property binding
2.2 registerOnChange
1 |
|
registerOnChange will be triggered when the data of the custom form control changes Method, this method is used to handle changes in custom form control data;
The parameter (fn) received by the registerOnChange method is actually a method, which is responsible for processing the changed data
When since When the defined control data changes, the method executed by fn will be automatically called, but the usual approach is to customize a method propagateChange and let the custom method point to fn, so that when the data changes, you only need to call propagateChange to process the changed data
2.3 registerOnTouched
1 |
|
The registerOnTouched method will be triggered when the form control is touched. The specific details will be updated...2018-1-31 11:18:33
2.4 setDisabledState
1 |
|
To be updated...2018-1-31 11:19:30
3 Programming steps
3.1 Create a custom form control component
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
3.1.1 Function description
When you click the increase button, the current count will increase by 1. When you click the decrease button, the current count will be cut by 1
3.1. 2 When used directly in other components, an error will be reported
The error message is as follows:
The error message is Say the component we use
3.2 How to turn the
3.2.1 Implement the ControlValueAccessor interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
3.2.2 Specify dependency information providers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
3.2.3 Bug to be fixed
Although it can run normally, the elements in the form control cannot accept the form model passed in the component using the form control The data and the data changed by the form control cannot be returned to the form model in the component that uses the form control; in short, there is no data interaction between the model and the view
3.3 Practice the data interaction between the model and the view
3.3.1 Model to view
Refactor the writeValue method in the custom form control class
Tip 01: The parameters in the writeValue method are passed in through the data binding of the form using the component that uses the custom form control
3.3.2 View Go to the model
》Customize a method to handle the changed data in the custom form control
1 |
|
》Reconstruct the registerOnChange method in the custom form control class
1 2 3 4 |
|
》Call the custom method where the data changes
3.4 Custom form control component code summary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
3.5 Code summary of the component that uses custom form controls
技巧01:如果自定义表单控件和使用自定义表单控件的组件都在不在同一个模块时需要对自定义表单控件对应组件进行导出和导入操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
3.6 初始化效果展示
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
JavaScript中Object基础内部方法图(图文教程)
The above is the detailed content of About the use of custom form controls in Angular19. For more information, please follow other related articles on the PHP Chinese website!