Created
September 24, 2013 16:37
-
-
Save victorb/6687484 to your computer and use it in GitHub Desktop.
Easy AngularJS Directive for Google Places Autocomplete
This file contains 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
var myApp = angular.module('myApp', []); | |
myApp.directive('googleplace', function() { | |
return { | |
require: 'ngModel', | |
link: function(scope, element, attrs, model) { | |
var options = { | |
types: [], | |
componentRestrictions: {} | |
}; | |
scope.gPlace = new google.maps.places.Autocomplete(element[0], options); | |
google.maps.event.addListener(scope.gPlace, 'place_changed', function() { | |
scope.$apply(function() { | |
model.$setViewValue(element.val()); | |
}); | |
}); | |
} | |
}; | |
}); | |
//myApp.factory('myService', function() {}); | |
function MyCtrl($scope) { | |
$scope.gPlace; | |
} |
This file contains 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
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script> | |
<div ng-app="myApp" ng-controller="MyCtrl"> | |
<div>Google Places Autocomplte integration in Angular</div> | |
<div>To Test, start typing the name of any Indian city</div> | |
<div>selection is: {{chosenPlace}}</div> | |
<div><input ng-model="chosenPlace" googleplace/></div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, this helped me a lot. Thank you for sharing this. Just to get some more information on this, is it possible that using this directive, i can have two places in a form which has this directive defined? I tried attaching this directive to two input elements and it only works for one. Anything on this would be a great help. Thanks.