我在我的网页上使用AjgularJS并希望添加一个字段以使用jqueryui中的自动完成功能,并且自动完成功能不会触发ajax调用。
我已经在没有角度(ng-app和ng-controller)的页面上测试过脚本并且效果很好,但是当我将脚本放在带有angularjs的页面上时它会停止工作。
任何想法?
jquery脚本:
$(function () {
$('#txtProduct').autocomplete({
source: function (request, response) {
alert(request.term);
},
minLength: 3,
select: function (event, ui) {
}
});
});
结论: 来自jQueryUI的自动完成小部件必须从AngularJS的自定义指令内部初始化,例如:
标记
<div ng-app="TestApp">
<h2>index</h2>
<div ng-controller="TestCtrl">
<input type="text" auto-complete>ddd</input>
</div>
</div>
Angular script
<script type="text/javascript">
var app = angular.module('TestApp', []);
function TestCtrl($scope) { }
app.directive('autoComplete', function () {
return function postLink(scope, iElement, iAttrs) {
$(function () {
$(iElement).autocomplete({
source: function (req, resp) {
alert(req.term);
}
});
});
}
});
</script>
答案 0 :(得分:36)
也许您只需要以“有角度的方式”进行操作......也就是说,使用指令设置DOM元素并进行事件绑定,使用服务获取数据,并使用控制器做你的业务逻辑......同时利用Angular的依赖注入优势......
获取自动填充数据的服务......
app.factory('autoCompleteDataService', [function() {
return {
getSource: function() {
//this is where you'd set up your source... could be an external source, I suppose. 'something.php'
return ['apples', 'oranges', 'bananas'];
}
}
}]);
执行设置自动完成插件工作的指令。
app.directive('autoComplete', function(autoCompleteDataService) {
return {
restrict: 'A',
link: function(scope, elem, attr, ctrl) {
// elem is a jquery lite object if jquery is not present,
// but with jquery and jquery ui, it will be a full jquery object.
elem.autocomplete({
source: autoCompleteDataService.getSource(), //from your service
minLength: 2
});
}
};
});
在你的标记中使用它...注意ng-model,用你选择的内容在$ scope上设置一个值。
<div ng-controller="Ctrl1">
<input type="text" ng-model="foo" auto-complete/>
Foo = {{foo}}
</div>
这只是基础知识,但希望有所帮助。
答案 1 :(得分:14)
我必须做更多的工作才能使用$ http服务。
服务:
app.factory("AutoCompleteService", ["$http", function ($http) {
return {
search: function (term) {
return $http.get("http://YourServiceUrl.com/" + term).then(function (response) {
return response.data;
});
}
};
}]);
指令:
app.directive("autocomplete", ["AutoCompleteService", function (AutoCompleteService) {
return {
restrict: "A",
link: function (scope, elem, attr, ctrl) {
elem.autocomplete({
source: function (searchTerm, response) {
AutoCompleteService.search(searchTerm.term).then(function (autocompleteResults) {
response($.map(autocompleteResults, function (autocompleteResult) {
return {
label: autocompleteResult.YourDisplayProperty,
value: autocompleteResult
}
}))
});
},
minLength: 3,
select: function (event, selectedItem) {
// Do something with the selected item, e.g.
scope.yourObject= selectedItem.item.value;
scope.$apply();
event.preventDefault();
}
});
}
};
}]);
html:
<input ng-model="YourObject" autocomplete />
答案 2 :(得分:0)
HTML
<input type="text" class="form-control ml-2" employeesearchautocomplete ng-model="employeeName" name="employeeName">
JS
var myApp = angular.module("employeeSearchModule",[]);
myApp.controller("employeeSearchController",function($scope,$http){
$scope.message= "Welcome to angular js..."
});
myApp.directive('employeesearchautocomplete', function() {
return {
restrict: 'A',
require : 'ngModel',
link : function (scope, element, attrs, ngModelCtrl) {
element.autocomplete({
source : function(request, response) {
$.ajax({
type : "POST",
url : "searchEmployee.html",
data : request,
success : response,
dataType : 'json'
});
},
select : function(event, ui) {
event.preventDefault();
if (ui.item.value == '-1') {
scope.employeeName ='';
scope.$apply();
} else {
scope.employeeName = ui.item.label;
scope.$apply();
}
},
focus : function(event, ui) {
event.preventDefault();
scope.employeeName = ui.item.label;
scope.$apply();
},
change : function(event, ui) {
if (!ui.item) {
scope.employeeName ='';
scope.$apply();
}
}
},{
minLength : 2
});
}
}
});
脚本文件导入顺序
我希望这会对某人有所帮助。