Angular: Can I attach an Observable to a variable? -
this doesn't seem work, yet there no errors:
@component({ selector: 'geofinder', template: '<input type="text" class="form-control" [(ngmodel)]="cityname" />' }) export class geofindercomponent implements oninit { cityname; ngoninit() { let citynamechange = observable.from(this.cityname); citynamechange.subscribe((data) => console.log(data)); } }
what trying achieve here attach observable variable triggers when value changes. i'd happy if bind observable keyup event of input field, refuses work.
you could...
use setter , add handling there
_cityname; cityname() { return this._cityname; } set cityname(name) { // stuff here }
you could...
split [(ngmodel)]
call callback (ngmodelchange)
hook
'<input [ngmodel]="cityname" (ngmodelchange)="onchange($event)"/>' cityname; onchange(newcity) { // stuff here }
you could...
use reactive forms if want observable. can hook valuechanges
(which observable
) of formcontrol
or formgroup
formcontrol = new formcontrol() formcontrol.valuechanges().debouncetime(..).filter(..)
if you've never used reactive forms before, should go through guide linked above.
Comments
Post a Comment