FormControl
to the rescue!!
FormControls and Reactive forms are great and it will save your day! :)
Mark ReactiveFormsModule
in your app.module, import FormControl
to your component aaand... For demo I used a simple timeout to change the value to trigger the valueChanges
:
myControl = new FormControl('')
constructor() { }
ngOnInit() {
setTimeout(() => {
this.myControl.setValue('something');
}, 2000);
this.myControl.valueChanges.subscribe((data) => {
console.log('changed!')
});
}
And template:
<input type="text" readonly [formControl]="myControl">
StackBlitz
P.S if this is input is part of a template driven form, this wouldn't work, since ngModel
and formControl
shouldn't be used together. So you would be notified of that. So in that case I suggest you go reactive way ;)