jQuery自动完成+ AngularJS的问题

问题描述 投票:19回答:3

我在我的页面上使用AjgularJS,并希望添加一个字段以使用来自jqueryui的自动完成功能,并且自动完成功能不会触发ajax调用。

我已经在没有angular的页面上测试了脚本(ng-app和ng-controller),并且效果很好,但是当我将脚本放在带有angularjs的页面上时,它将停止工作。

任何想法?

jquery脚本:

$(function () {

    $('#txtProduct').autocomplete({
        source: function (request, response) {

            alert(request.term);

        },
        minLength: 3,
        select: function (event, ui) {

        }
    });

});
  • 有趣的提示:当我在Chrome检查器中调用脚本时,自动完成功能开始工作!!
  • 版本:AngularJS:1.0.2-JqueryUI:1.9.0

结论:jQueryUI的自动完成小部件必须从AngularJS的自定义指令内部进行初始化,例如:

标记

<div ng-app="TestApp">
    <h2>index</h2>
    <div ng-controller="TestCtrl">

        <input type="text" auto-complete>ddd</input>

    </div>
</div>

Angular脚本

<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>
jquery jquery-ui angularjs jquery-autocomplete
3个回答
37
投票
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模型使用您选择的内容在$ scope上设置一个值。

<div ng-controller="Ctrl1"> <input type="text" ng-model="foo" auto-complete/> Foo = {{foo}} </div>

这只是基础,但希望能有所帮助。

14
投票
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 />


0
投票
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 }); } } });

脚本文件导入顺序

您所有的jquery

AngularJs库

    您的自动完成的角度脚本
  • 我希望这会对某人有所帮助。
  • © www.soinside.com 2019 - 2024. All rights reserved.