Thank you in advance for your help.
I have been trying to get an autocomplete search box setup to interact with the google AutocompleteService API using angular.
At the moment my Angular AutocompleteService is working well and retrieving an array of suggested predictions based on what is typed into the search bar.
Here is my Autocomplete Service:
angular
.module('LocalKnowledgeApp')
.service('AutocompleteService', [ function() {
var self = this;
var suggestions = [];
self.initPredict = function(searchInput) {
var predict = new google.maps.places.AutocompleteService();
predict.getQueryPredictions({ input: searchInput }, self.displaySuggestions);
};
self.displaySuggestions = function(predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
console.log(status);
return;
}
predictions.forEach(function(prediction) {
suggestions.push(prediction.description);
});
};
self.makeSuggestions = function(){
return suggestions.slice(0,10);
};
That service is injected into a Controller which controls the form I am trying to connect to the autocomplete. (I've only included the function that is involved in the autocomplete bit and interacts with the AutocompleteService)
angular
.module('LocalKnowledgeApp')
.controller('RequestController', ['LocationService', 'MarkersService', 'AutocompleteService', '$http', '$scope',
function(LocationService, MarkersService, AutocompleteService, $http, $scope) {
var self = this;
var isInfoOpen = false;
var clickedRequest = {};
var requestUser = {};
var autocompleteStarted;
self.master = {};
var autocompleteSuggestions = [];
// var search = autoCompleteSearch;
self.autoCompleteSearch = function(searchInput){
if (searchInput.length < 2) {
self.autocompleteStarted = false;
} else {
AutocompleteService.initPredict(searchInput);
self.autocompleteStarted = true;
self.autocompleteSuggestions = AutocompleteService.makeSuggestions();
return self.autocompleteSuggestions;
}
};
I am now trying to wire up to get a drop-down box on the search bar so that users can then choose from the array of suggestions that the Google Autocomplete service returns to the RequestsController.
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content" >
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Request a tour from a local</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="location">Location</label>
<div>
<md-autocomplete
md-search-text-change="r.autoCompleteSearch(searchText)"
md-input-name="request.location"
md-selected-item="selectedItem"
md-search-text="searchText"
md-items="item in r.autocompleteSearch(searchText)"
md-item-text="item.display"
md-input-minlength="0">
<md-item-template md-highlight-text="searchText">
{{item.display}}
</md-item-template>
</div>
</md-autocomplete>
<aside ng-show="r.autocompleteStarted" id="autocompleteSuggestions">
<ul ng-repeat="suggestion in r.autocompleteSuggestions track by $index"> {{suggestion}} </ul>
<aside>
</div>
I have injected the ngMaterial module, and have all the bower components required in the right place - I know this because the directive is responsive to input, but it only shows a grey box.
(function() {
'use strict';
angular
.module('LocalKnowledgeApp', ['ngResource', 'ngMaterial']);
}());
This is what my webpage is looking like...
[
Any help much appreciated!