Created
May 24, 2022 09:09
-
-
Save dcagnetta/4a182bb5728b167db2d104069c5358a7 to your computer and use it in GitHub Desktop.
Control Value Accessor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button (click)="updateValue(+value - 1)">-</button> | |
<input [ngModel]="value" | |
(ngModelChange)="updateValue($event)" | |
type="string"> | |
<button (click)="updateValue(+value + 1)">+</button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<app-quantity-input [(ngModel)]="quantity"></app-quantity-input> | |
<div> | |
Quantity: {{ quantity }} | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button (click)="updateValue(+value - 1)">-</button> | |
<input [ngModel]="value" | |
(ngModelChange)="updateValue($event)" | |
type="string"> | |
<button (click)="updateValue(+value + 1)">+</button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, forwardRef } from '@angular/core'; | |
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | |
import { Subject } from 'rxjs'; | |
@Component({ | |
selector: 'app-quantity-input', | |
templateUrl: './quantity-input.component.html', | |
providers: [{ | |
provide: NG_VALUE_ACCESSOR, | |
useExisting: forwardRef(() => QuantityInputComponent), | |
multi: true, | |
}], | |
}) | |
export class QuantityInputComponent implements ControlValueAccessor { | |
value: number; | |
disabled = false; | |
private valueChanges = new Subject<number>(); | |
private touches = new Subject(); | |
registerOnChange(fn: any) { | |
console.log('registerOnChange',fn); | |
this.valueChanges.subscribe(fn); | |
} | |
registerOnTouched(fn: any) { | |
this.touches.subscribe(fn); | |
} | |
setDisabledState(isDisabled: boolean) { | |
this.disabled = isDisabled; | |
} | |
writeValue(value: number) { | |
this.value = value; | |
} | |
updateValue(value: number) { | |
this.value = value; | |
this.valueChanges.next(value); | |
} | |
touch() { | |
this.touches.next(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjVkIHd4vf3AhWJfMAKHeM7C1oQFnoECAQQAQ&url=https%3A%2F%2Fstackblitz.com%2Fedit%2Fangular-cva&usg=AOvVaw0Taz1DZEMJQedkITmNZlcw