Created
December 20, 2016 14:29
-
-
Save cod3cow/36d151bb5f0946291cb43d56f1d3a486 to your computer and use it in GitHub Desktop.
google place directive for angular 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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