diff --git a/ngAutocomplete.js b/ngAutocomplete.js new file mode 100644 index 0000000..fe1be2c --- /dev/null +++ b/ngAutocomplete.js @@ -0,0 +1,169 @@ +'use strict'; + +/** + * A directive for adding google places autocomplete to a text box + * google places autocomplete info: https://developers.google.com/maps/documentation/javascript/places + * + * Usage: + * + * 0){ + autocompleteService.getPlacePredictions( + { + input: result.name, + offset: result.name.length + }, + function listentoresult(list, status) { + if(list == null || list.length == 0) { + + scope.$apply(function() { + scope.details = null; + }); + + } else { + var placesService = new google.maps.places.PlacesService(element[0]); + placesService.getDetails( + {'reference': list[0].reference}, + function detailsresult(detailsResult, placesServiceStatus) { + + if (placesServiceStatus == google.maps.GeocoderStatus.OK) { + scope.$apply(function() { + + controller.$setViewValue(detailsResult.formatted_address); + element.val(detailsResult.formatted_address); + + scope.details = detailsResult; + + //on focusout the value reverts, need to set it again. + var watchFocusOut = element.on('focusout', function(event) { + element.val(detailsResult.formatted_address); + element.unbind('focusout') + }) + + }); + } + } + ); + } + }); + } + } + + controller.$render = function () { + var location = controller.$viewValue; + element.val(location); + }; + + //watch options provided to directive + scope.watchOptions = function () { + return scope.options + }; + scope.$watch(scope.watchOptions, function () { + initOpts() + }, true); + + } + }; + }); \ No newline at end of file