Skip to content

Instantly share code, notes, and snippets.

@cod3cow
Created December 20, 2016 14:29
Show Gist options
  • Save cod3cow/36d151bb5f0946291cb43d56f1d3a486 to your computer and use it in GitHub Desktop.
Save cod3cow/36d151bb5f0946291cb43d56f1d3a486 to your computer and use it in GitHub Desktop.
google place directive for angular 2
import {Directive, ElementRef, EventEmitter, Output} from '@angular/core';
import {NgModel} from '@angular/forms';
declare var google:any;
@Directive({
selector: '[googleplace]',
providers: [NgModel],
host: {
'(input)' : 'onInputChange()'
}
})
export class GoogleplaceDirective {
@Output() setAddress: EventEmitter<any> = new EventEmitter();
modelValue:any;
autocomplete:any;
private _el:HTMLElement;
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', ()=> {
var place = this.autocomplete.getPlace();
this.invokeEvent(place);
});
}
invokeEvent(place:Object) {
this.setAddress.emit(place);
}
onInputChange() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment