【发布时间】:2016-05-03 19:24:01
【问题描述】:
我必须为国家下拉菜单实现自动完成。我使用的是 angularjs 1.4 版本。
我参考了这个网站click here 来实现这个自动完成。
但它在实现上述代码时显示此错误“iElement.autocomplete is not a function”。是否有任何 js 文件我必须包含
这是我的html代码
<div ng-app='MyModule'>
<div ng-controller='DefaultCtrl'>
<input auto-complete ui-items="countries" ng-model="selected">
selected = {{selected}}
</div>
</div>
js文件
var app = angular.module('MyModule', []);
app.controller('DefaultCtrl', function($scope)
{
$scope.countries = [
{name: 'Afghanistan', code: 'AF'},
{name: 'Antigua and Barbuda', code: 'AG'},
{name: 'Bahamas', code: 'BS'},
{name: 'Cambodia', code: 'KH'},
{name: 'Cape Verde', code: 'CV'}];
});
app.directive('autoComplete', function($timeout) {
return function(scope, iElement, iAttrs) {
iElement.autocomplete({
source: scope[iAttrs.uiItems],
select: function() {
$timeout(function() {
iElement.trigger('input');
}, 0);
}
});
};
});
提前谢谢你
【问题讨论】:
-
由于 iElement 基本上是指 元素,而 默认情况下没有任何此类方法,因此您会收到此错误。似乎需要在应用程序声明中注入一个依赖项。或者您可能错过了包括所需的脚本。