Problems with jQuery autocomplete + AngularJS

后端 未结 3 1957
夕颜
夕颜 2020-12-02 11:35

i\'m using AjgularJS on my page and want to add a field to use autocomplete from jqueryui and the autocomplete does not fires the ajax call.

i\'ve tested the script

3条回答
  •  情话喂你
    2020-12-02 12:03

    I had to do a bit more work to get this working using an $http service.

    The service:

    app.factory("AutoCompleteService", ["$http", function ($http) {
        return {
            search: function (term) {
                return $http.get("http://YourServiceUrl.com/" + term).then(function (response) {
                    return response.data;
                });
            }
        };
    }]);
    

    The directive:

    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();
                    }
                });
            }
        };
    }]);
    

    The html:

    
    

提交回复
热议问题