I have a google place directive; when I type on my input field it suggests places and upon selection it outputs the place name. I see the selected value in input field, but my form control value shows only what I typed in the input field. I am using model driven form. For example: if I type bha and then select Bharat Diamond House, the input field shows correctly Bharat Diamond House but the form value gets only bha. My problem is similar to this and this
My directive looks like this:
import {Directive, ElementRef, EventEmitter, Output} from '@angular/core';
import {NgModel} from '@angular/forms';
import {Address} from './model/google_place';
declare var google:any;
@Directive({
selector: '[googleplace]',
providers: [NgModel],
host: {
'(input)' : 'onInputChange($event)'
}
})
export class GoogleplaceDirective {
@Output() setAddress: EventEmitter<any> = new EventEmitter();
@Output() adr_address: EventEmitter<any> = new EventEmitter();
@Output() place_id: EventEmitter<any> = new EventEmitter();
@Output() formatted_address: EventEmitter<any> = new EventEmitter();
modelValue:any;
autocomplete:any;
private _el:HTMLElement;
place:Address;
constructor(el: ElementRef, private model:NgModel) {
this._el = el.nativeElement;
this.modelValue = this.model;
var input = this._el;
this.autocomplete = new google.maps.places.Autocomplete(input, {});
google.maps.event.addListener(this.autocomplete, 'place_changed', ()=> {
(<HTMLInputElement>input).value=(<HTMLInputElement>input).value.split(',')[0];
this.place = this.autocomplete.getPlace();
if (this.place && this.place.place_id){
this.invokeEvent();
}
});
}
//invokeEvent(place:Object) {
invokeEvent() {
this.setAddress.emit(this.place);
this.adr_address.emit(this.place.adr_address ? this.place.adr_address : null);
this.formatted_address.emit(this.place.formatted_address ? (this.place.formatted_address) : null);
}
onInputChange() {
}
}