Fla*_*ira 19 jquery jquery-ui jquery-autocomplete angularjs
我在我的页面上使用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) {
}
});
});
Run Code Online (Sandbox Code Playgroud)
结论: 来自jQueryUI的自动完成小部件必须从AngularJS的自定义指令内部初始化,例如:
标记
<div ng-app="TestApp">
<h2>index</h2>
<div ng-controller="TestCtrl">
<input type="text" auto-complete>ddd</input>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
角度脚本
<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>
Run Code Online (Sandbox Code Playgroud)
Ben*_*esh 37
也许您只需要以"有角度的方式"进行操作......也就是说,使用指令设置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'];
}
}
}]);
Run Code Online (Sandbox Code Playgroud)
用于设置自动完成插件的指令.
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
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
并在标记中使用它...注意ng-model,用你选择的内容在$ scope上设置一个值.
<div ng-controller="Ctrl1">
<input type="text" ng-model="foo" auto-complete/>
Foo = {{foo}}
</div>
Run Code Online (Sandbox Code Playgroud)
这只是基础知识,但希望这有所帮助.
Jas*_*son 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;
});
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
指令:
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();
}
});
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
html:
<input ng-model="YourObject" autocomplete />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29617 次 |
| 最近记录: |