我正在使用"自动完成" jsfiddle的指令,错误为iElement.autocomplete is not a function
。
请帮我解决这个错误。
directive.js
starter.directive('autoComplete', function($timeout) {
return function(scope, iElement, iAttrs) {
iElement.autocomplete({
source: function(request, response) {
var res = new Array()
for (var i=0; i<scope[iAttrs.uiItems].length; i++) {
if (scope[iAttrs.uiItems][i].indexOf(request.term) == 0) {
res.push(scope[iAttrs.uiItems][i]);
}
}
response(res);
},
select: function() {
$timeout(function() {
iElement.trigger('input');
}, 0);
}
});
};
});
.html文件
<input type="text" auto-complete="true" ui-items="names" ng-model="selected" class="tagdiv" style="color:#fff" placeholder="Tag your category">
.js文件
$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"];
答案 0 :(得分:0)
首先,在你的jsfiddle中你没有包含jquery-ui javascript,这会导致你提到的错误。
此外,您的指令的实现是错误的。该指令应该返回一个带有指令配置的对象,而不是函数:
starter.directive('autoComplete', function($timeout) {
return {
restrict: "AE",
scope: {
uiItems: "="
},
link: function(scope, iElement, iAttrs) {
iElement.autocomplete({
// not sure why you had a whole function here, this should do the trick:
source: scope.uiItems,
select: function() {
$timeout(function() {
iElement.trigger('input');
}, 0);
}
});
}
}
});