Why when updating ControlValueAccessor you do not start ngOnChanges

Why doesn't nGOnChanges start when you "writeValue" in ControlValueAccessor? I emit some events through registerOnChange, but I don't see any action in ngOnChanges

const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR = new Provider( NG_VALUE_ACCESSOR, { useExisting: forwardRef(() => CustomInput), multi: true }); @Component({ moduleId: module.id, selector: 'custom-input', templateUrl: 'custom-input.html', styleUrls: ['custom-input.css'], directives: [CORE_DIRECTIVES], providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR] }) export class CustomInput implements ControlValueAccessor{ private _text: any = ''; public isValid:boolean = false; onChange: EventEmitter<any> = new EventEmitter(); onTouched: any; constructor(private ngControl:NgControl) { } ngOnChanges(e) { console.log('ngOnChanges'); } get text(): any { return this._text; }; set text(v: any) { if (v !== this._text) { this._text = v; this.onChange.emit(v); } } writeValue(v) { this.text = v; } registerOnChange(fn): void { this.onChange.subscribe(fn); } registerOnTouched(fn): void { this.onTouched = fn; } } 
+5
source share
1 answer

ngOnChanges is called when the @Input() properties are @Input() .

Suppose your user input has a text property:

 export class CustomInput implements OnChanges { @Input() text; ngOnChanges() { console.log('ngOnChanges'); } } 

Then ngOnChanges is called when someVariable changes:

 <custom-input [text]="someVariable"></custom-input> 

Changes made from a component do not cause a callback. ngOnChanges purely for Inputs.

https://angular.io/guide/lifecycle-hooks#lifecycle-sequence


Note: ngOnChanges also called before ngOnInit

+1
source

All Articles