我正在开发基于AngularJs的SPA。搜索框具有自动完成功能,该功能无法正常工作。当我开始在搜索框中输入内容时,{{search.name}}中会显示输入的字符串并显示建议,但当我点击其中一条建议时,它不会显示在{{search.name}}中。如果按下任何其他按钮,则会出现字符串。
这是我的HTML代码
<div class=" upper-one">
<div class="search-box" style="display:inline;">
<img src="img/search.png" width="28px" height="28px" style="float:left;">
<input id="tags" class="input-search-box" type="text" placeholder="Start typing " data-ng-model="search.name"></input>
<a> <div class="go-class" style="position:relative;" data-ng-click="searchProduct()"> Go</div> </a>
</div>
{{search.name}
</div>
这是我的jquery代码
<script >
$(function() {
var availableTags =["Jquery","Html","Css"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
我的代码出了什么问题?
答案 0 :(得分:1)
只需使用Angular自动完成功能即可解决此问题。
function DefaultCtrl($scope) {
$scope.names = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"];}
angular.module('MyModule', []).directive('autoComplete', function($timeout) {
return function(scope, iElement, iAttrs) {
iElement.autocomplete({
source: scope[iAttrs.uiItems],
select: function() {
$timeout(function() {
iElement.trigger('input');
}, 0);
}
});
};
});
<div ng-app='MyModule'>
<div ng-controller='DefaultCtrl'>
<input auto-complete ui-items="names" ng-model="selected">
selected = {{selected}}
</div>