0

I tried to implement Textbox auto-complete with "autocomplete" directive of Angular but it is not recognized by application. Here is my app:

var app = angular.module('app', [
'ngRoute',
'ngCookies',

]);

app.service('AutoCompleteService', ['$http', function ($http) {
return {
    search: function (term) {
        return $http.get('https://myapi.net/suggest?query='+term+'&subscription-key=XYZ').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.ID,
                            value: autocompleteResult.Val
                        }
                    }))
                });
            },
            minLength: 3,
            select: function (event, selectedItem) {
                // Do something with the selected item, e.g. 
                scope.yourObject = selectedItem.item.value;
                scope.$apply();
                event.preventDefault();
            }
        });
    }
};

}]);

And I placed directive name as follows:

<input type="text" id="search" ng-model="searchText" placeholder="Enter Search Text" autocomplete />

Still AutoCompleteService is not called by directive. Am I doing anything wrong here?

1 Answer 1

1

It is not that your service is not called from the directive, it is that your directive is not called at all based on the html you've provided. You should call a directive by transforming camel case notation to a dashed notation, as follows :

<input id="search" ng-model="searchText" auto-complete />

You can find all about directive matching in the AngularJS Directive Documentation.

Sign up to request clarification or add additional context in comments.

6 Comments

Actually when I am writing it as "auto-complete" I am getting error message as: **Error: i.autocomplete is not a function ** .
Yes, and that is because "auto-complete" is the right way to call your directive, which explains why you did not get the error when you called your directive with "autocomplete" as in that case the directive code was not interpreted at all. So when it was successfully called in the first case, it did not find the autocomplete function. Are you sure you included jQuery as well as jQuery-UI ?
Thanks for pointing that, I did not include jquery and jquery-UI but after including that the above error is gone but still autocomplete is not working.
You can try to replace your source function with a simpler one as follows : source: function (searchTerm, response) { return ['aaa', 'bbb']; } Also, do not forget that you need to type at least 3 characters for the autocomplete to trigger. If the autocomplete triggered after three characters then you would know that the issue comes from your service.
Thanks for suggesting that! I replaced the source with simpler form but autocomplete did not trigger. Is bootstrap-UI is problamatic?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.