Guest User

Untitled

a guest
Oct 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. import { forwardRef } from '@angular/core';
  2. import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
  3.  
  4. export abstract class AbstractValueAccessor implements ControlValueAccessor {
  5. val: any = '';
  6.  
  7. get value (): any { return this.val; }
  8. set value (v: any) {
  9. if (v !== this.val) {
  10. this.val = v;
  11. this.onChange(v);
  12. }
  13. }
  14.  
  15. writeValue (value: any) {
  16. this.val = value;
  17. this.onChange(value);
  18. }
  19.  
  20. onChange = (_) => { };
  21.  
  22. onTouched = () => { };
  23.  
  24. registerOnChange (fn: (_: any) => void): void { this.onChange = fn; }
  25.  
  26. registerOnTouched (fn: () => void): void { this.onTouched = fn; }
  27. }
  28.  
  29. export function MakeProvider (type: any) {
  30. return {
  31. provide: NG_VALUE_ACCESSOR,
  32. useExisting: forwardRef(() => type),
  33. multi: true
  34. };
  35. }
Add Comment
Please, Sign In to add comment