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
|
import { Component, forwardRef, OnDestroy, ChangeDetectionStrategy } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, FormBuilder, FormGroup, Validators, FormControl, NG_VALIDATORS } from '@angular/forms';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-civilite',
templateUrl: './civilite.component.html',
styleUrls: ['./civilite.component.less'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CiviliteComponent),
multi: true
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => CiviliteComponent),
multi: true,
}
],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CiviliteComponent {
FGcivilite;
subscriptions: Subscription[] = [];
get value() {
return this.FGcivilite.value;
}
set value(value) {
this.FGcivilite.setValue(value);
this.onChange(value);
this.onTouched();
}
constructor(private formBuilder: FormBuilder) {
this.FGcivilite = this.formBuilder.group({
prenom: ['', Validators.required],
nom: ['', Validators.required],
naissance: ['', Validators.required],
});
this.subscriptions.push(
this.FGcivilite.valueChanges.subscribe(value => {
this.onChange(value);
this.onTouched();
})
);
}
validate(_: FormControl) {
return this.FGcivilite.valid ? null : { FCcivilite: { valid: false, }, };
}
ngOnDestroy() {
this.subscriptions.forEach(s => s.unsubscribe());
}
onChange: any = () => { };
onTouched: any = () => { };
registerOnChange(fn) {
this.onChange = fn;
}
writeValue(value) {
if (value) {
this.FGcivilite = value;
}
if (value === null) {
this.FGcivilite.reset();
}
}
registerOnTouched(fn) {
this.onTouched = fn;
}
} |