‘@angular/core‘;import { ControlValueAccessor, NG_VALUE_ACCESSOR } from ‘@angular/forms‘;const COUNTER_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => StockCounterComponent), multi: true};@Component({ selector: ‘stock-counter‘, providers: [COUNTER_VALUE_ACCESSOR], styleUrls: [‘stock-counter。
component。scss‘], template: ` 。 `})export class StockCounterComponent implements ControlValueAccessor { value: number; writeValue(val: number) { this。
value = val; } registerOnChange(fn: any) { this。onModelChange = fn; } registerOnTouched(fn: any) { this。onTouch = fn; } 。
}So the template is somehow like the code above, highly recommend to create any snippet or live template in your IDE。So inside the component we create, we have three methods,writeValue(obj: any)registerOnChange(fn: Function)registerOnTouched(fn: Function)To understand each method is the key point to understand how control value access。
So first ‘writeValue‘:value: number; writeValue(val: number) { this。value = val; }This is for setting initial value, we take value from our form builder and pass to our component via ‘writeValue‘。
<stock-counter [step]=”10″ [min]=”10″ [max]=”1000″ formControlName=”quantity”>So the value comes from ‘formControlName=”quantity”‘。
Now at the point, you can think that our form holds a model value and our component also holds a view value。We need to sync model value and view value。
The way to do that is by “registerOnChange”:registerOnChange(fn: any) { this。onModelChange = fn; } increment() { this。value = this。
value === this。max ? this。value : this。value + this。step; this。onModelChange(this。value); }Every time our view value (component value) changed, to need to notify our form about the changes, we need to call ‘this。
onModelChange(this。value)‘ function and pass in the changes value。After this form will be able to the updated value。OK, now we able to sync the value from our component to our form。
But you might think about this can be easily done by EventEmitter, when border to create Control value accessor? The most important reason is that “Validation”!html “form” component actually does lots of thing underhook。
What example, it set field state “untouch”, “touched”, “dirty”, “prinstin”。 We use those status to do validation and error messages。
For example:input。ng-touched。ng-invalid { border-left: 5px solid red;}If the field is touched and is invalid, we set the border to red。
Currently, we have our value synced, and control value access also helps us to add form validation ability to our component。
But once the value changed, our component still show ‘ng-untouch‘:So we need “registerOnTouched” function to help us to do that:registerOnTouched(fn: any) { this。
onTouch = fn; } increment() { this。value = this。value === this。max ? this。value : this。value + this。step; this。onModelChange(this。
value); this。onTouch(); }Now after the value changed, our component will be set ‘ng-touched‘, now we are fully conver our component to a form component。
Code[Angular] Implementing a ControlValueAccessor。
1.文章《[Angular] Implementing a ControlValueAccessor》援引自互联网,为网友投稿收集整理,仅供学习和研究使用,内容仅代表作者本人观点,与本网站无关,侵删请点击页脚联系方式。
2.文章《[Angular] Implementing a ControlValueAccessor》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。
相关推荐
- . 现代买票为什么带上携程保险
- . 潮阳怎么去广州南站
- . 湖南马拉河怎么样
- . 烧纸为什么到三岔路口
- . 百色为什么这么热
- . 神州租车怎么样
- . 芜湖方特哪个适合儿童
- . 护肤品保养液是什么类目
- . 早晚的护肤保养有哪些项目
- . 女孩护肤品怎么保养的最好