预先感谢您的帮助。
我一直在尝试设置自动完成搜索框,以使用 Angular 与 google AutocompleteService API 进行交互。
目前,我的 Angular AutocompleteService 运行良好,并根据在搜索栏中输入的内容检索一系列建议的预测。
这是我的自动完成服务:
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);
};
该服务被注入到一个控制器中,该控制器控制我试图连接到自动完成的表单。(我只包含了涉及自动完成位并与 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;
}
};
我现在正在尝试连接以在搜索栏上获取一个下拉框,以便用户可以从 Google 自动完成服务返回给 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>
我已经注入了 ngMaterial 模块,并且在正确的位置拥有了所有需要的 bower 组件——我知道这一点是因为指令响应输入,但它只显示一个灰色框。
(function() {
'use strict';
angular
.module('LocalKnowledgeApp', ['ngResource', 'ngMaterial']);
}());
这就是我的网页的样子......
[
非常感谢任何帮助!