├── bower.json ├── examples ├── advanced.css ├── advanced.js └── advanced.html ├── README.md └── src └── ngAutocomplete.js /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ngAutocomplete", 3 | "version": "1.0.0", 4 | "main": "src/ngAutocomplete.js", 5 | "dependencies": { 6 | "angular": ">=1.2.0" 7 | } 8 | } -------------------------------------------------------------------------------- /examples/advanced.css: -------------------------------------------------------------------------------- 1 | #form { 2 | 3 | margin-left: 100px; 4 | width: 500px 5 | } 6 | 7 | .indent { 8 | 9 | margin-left: 50px; 10 | } 11 | 12 | .move-down { 13 | margin-top: 100px; 14 | } -------------------------------------------------------------------------------- /examples/advanced.js: -------------------------------------------------------------------------------- 1 | angular.module( "Test", ['ngAutocomplete']) 2 | .controller("TestCtrl",function ($scope) { 3 | 4 | $scope.result = '' 5 | // $scope.details = '' 6 | $scope.options = {}; 7 | 8 | $scope.form = { 9 | type: 'geocode', 10 | bounds: {SWLat: 49, SWLng: -97, NELat: 50, NELng: -96}, 11 | country: 'ca', 12 | typesEnabled: false, 13 | boundsEnabled: false, 14 | componentEnabled: false, 15 | watchEnter: true 16 | } 17 | 18 | //watch form for changes 19 | $scope.watchForm = function () { 20 | return $scope.form 21 | }; 22 | $scope.$watch($scope.watchForm, function () { 23 | $scope.checkForm() 24 | }, true); 25 | 26 | 27 | //set options from form selections 28 | $scope.checkForm = function() { 29 | 30 | $scope.options = {}; 31 | 32 | $scope.options.watchEnter = $scope.form.watchEnter 33 | 34 | if ($scope.form.typesEnabled) { 35 | $scope.options.types = $scope.form.type 36 | } 37 | if ($scope.form.boundsEnabled) { 38 | 39 | var SW = new google.maps.LatLng($scope.form.bounds.SWLat, $scope.form.bounds.SWLng) 40 | var NE = new google.maps.LatLng($scope.form.bounds.NELat, $scope.form.bounds.NELng) 41 | var bounds = new google.maps.LatLngBounds(SW, NE); 42 | $scope.options.bounds = bounds 43 | 44 | } 45 | if ($scope.form.componentEnabled) { 46 | $scope.options.country = $scope.form.country 47 | } 48 | }; 49 | 50 | }); 51 | 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ng-Autocomplete 2 | 3 | A simple directive for adding google places autocomplete to a textbox element. 4 | 5 | Updated to now use ng-model, should work much better in forms. Can now set an initial value using ng-model. Using the ng-model to set the textbox value does not trigger the autocomplete query. 6 | 7 | Tested with angularjs-1.2.4 8 | 9 | Uses optional directive parameters, so it won't work with <1.2. If people are interested I'll release a <1.2 version. 10 | 11 | ## Examples 12 | 13 | + [Example Plunkers - Simple Usage](http://plnkr.co/edit/GE34ojss9xMGm0024FvM?p=preview) 14 | 15 | + [Example Plunkers - Advanced Usage](http://plnkr.co/edit/GF3nM3XfYX9El2w11pGo?p=preview) 16 | 17 | ## Usage 18 | 19 | Include the required libraries 20 | ```html 21 | 22 | ``` 23 | 24 | Declare a dependency on the `ngAutocomplete` module 25 | ``` javascript 26 | var app = angular.module('myModule', ['ngAutocomplete']); 27 | ``` 28 | 29 | Add the directive to a textbox 30 | 31 | ``` javascript 32 | 33 | ``` 34 | 35 | ## Documentation 36 | 37 | + ng-model - autocomplete textbox value 38 | 39 | + details - more detailed autocomplete result, includes address parts, latlng, etc. (Optional) 40 | 41 | + options - configuration for the autocomplete (Optional) 42 | 43 | + types: type, String, values can be 'geocode', 'establishment', '(regions)', or '(cities)' 44 | + bounds: bounds, Google maps LatLngBounds Object, biases results to bounds, but may return results outside these bounds 45 | + country: country String, ISO 3166-1 Alpha-2 compatible country code. examples; 'ca', 'us', 'gb' 46 | + watchEnter: Boolean, true; on Enter select top autocomplete result. false(default); enter ends autocomplete 47 | 48 | example: 49 | ``` javascript 50 | options = { 51 | types: '(cities)', 52 | country: 'ca' 53 | } 54 | ``` 55 | 56 | google places autocomplete info: https://developers.google.com/maps/documentation/javascript/places 57 | 58 | ## Author 59 | 60 | **Will Palahnuk** (http://github.com/wpalahnuk) 61 | 62 | ## Credits 63 | 64 | google places autocomplete https://developers.google.com/maps/documentation/javascript/places 65 | 66 | ## Copyright and license 67 | 68 | The MIT License 69 | 70 | Copyright (c) 2014 Will Palahnuk 71 | 72 | Permission is hereby granted, free of charge, to any person obtaining a copy 73 | of this software and associated documentation files (the "Software"), to deal 74 | in the Software without restriction, including without limitation the rights 75 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 76 | copies of the Software, and to permit persons to whom the Software is 77 | furnished to do so, subject to the following conditions: 78 | 79 | The above copyright notice and this permission notice shall be included in 80 | all copies or substantial portions of the Software. 81 | 82 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 83 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 84 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 85 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 86 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 87 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 88 | THE SOFTWARE. 89 | -------------------------------------------------------------------------------- /examples/advanced.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 |
17 | 18 |
19 | 23 |
24 | 25 | 26 |
27 |
28 | 32 |
33 |
34 | 38 |
39 |
40 | 44 |
45 |
46 | 50 |
51 |
52 | 53 |
54 | 58 |
59 | 60 |
61 |
Southwest Corner
62 | 66 | 70 |
Northeast Corner
71 | 75 | 79 |
80 | 81 | 82 |
83 | 87 |
88 |
89 | 94 |
95 |

96 |
97 | 101 |
102 | 103 | 104 |
105 | 106 | 107 | 108 |
109 | autocomplete: {{autocomplete}} 110 |

111 | details (formatted address): {{details.formatted_address}} 112 | 113 |
114 | 115 |
116 | 117 |
118 | 119 |
120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 |
129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /src/ngAutocomplete.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * A directive for adding google places autocomplete to a text box 5 | * google places autocomplete info: https://developers.google.com/maps/documentation/javascript/places 6 | * 7 | * Usage: 8 | * 9 | * 0){ 110 | autocompleteService.getPlacePredictions( 111 | { 112 | input: result.name, 113 | offset: result.name.length 114 | }, 115 | function listentoresult(list, status) { 116 | if(list == null || list.length == 0) { 117 | 118 | scope.$apply(function() { 119 | scope.details = null; 120 | }); 121 | 122 | } else { 123 | var placesService = new google.maps.places.PlacesService(element[0]); 124 | placesService.getDetails( 125 | {'reference': list[0].reference}, 126 | function detailsresult(detailsResult, placesServiceStatus) { 127 | 128 | if (placesServiceStatus == google.maps.GeocoderStatus.OK) { 129 | scope.$apply(function() { 130 | 131 | controller.$setViewValue(detailsResult.formatted_address); 132 | element.val(detailsResult.formatted_address); 133 | 134 | scope.details = detailsResult; 135 | 136 | //on focusout the value reverts, need to set it again. 137 | var watchFocusOut = element.on('focusout', function(event) { 138 | element.val(detailsResult.formatted_address); 139 | element.unbind('focusout') 140 | }) 141 | 142 | }); 143 | } 144 | } 145 | ); 146 | } 147 | }); 148 | } 149 | } 150 | 151 | controller.$render = function () { 152 | var location = controller.$viewValue; 153 | element.val(location); 154 | }; 155 | 156 | //watch options provided to directive 157 | scope.watchOptions = function () { 158 | return scope.options 159 | }; 160 | scope.$watch(scope.watchOptions, function () { 161 | initOpts() 162 | }, true); 163 | 164 | } 165 | }; 166 | }); --------------------------------------------------------------------------------