Created
January 18, 2014 18:14
-
-
Save andybak/8494098 to your computer and use it in GitHub Desktop.
Angular typeahead snippet.
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
<div ng-controller="TypeaheadCtrl"> | |
<pre>Model: {{selected | json}}</pre> | |
<input type="text" ng-model="selected" typeahead="state for state in states | filter:$viewValue | limitTo:8" class="form-control"> | |
<pre>Model: {{asyncSelected | json}}</pre> | |
<input type="text" ng-model="asyncSelected" placeholder="Locations loaded via $http" typeahead="address for address in getLocation($viewValue) | filter:$viewValue" typeahead-loading="loadingLocations" class="form-control"> | |
<i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i> | |
</div> |
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
function TypeaheadCtrl($scope, $http) { | |
$scope.selected = undefined; | |
$scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']; | |
// Any function returning a promise object can be used to load values asynchronously | |
$scope.getLocation = function(val) { | |
return $http.get('http://maps.googleapis.com/maps/api/geocode/json', { | |
params: { | |
address: val, | |
sensor: false | |
} | |
}).then(function(res){ | |
var addresses = []; | |
angular.forEach(res.data.results, function(item){ | |
addresses.push(item.formatted_address); | |
}); | |
return addresses; | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment