0

I am trying to make autocomplete on input Text search field with help of jquery.autocomplete.js in angularjs. But it is not working.I checked firebug console there is no error.Please help me.

code

 app.directive('autoComplete', function(autoCompleteDataService) {
    return {
            restrict: 'A',
           link: function(scope, elem, attr, ctrl) {

        elem.autocompleteArray({
            source: autoCompleteDataService.getSource(), 
            minLength: 2
        });
    }
};

});

app.factory('autoCompleteDataService', [function() {
    return {
        getSource: function() {

        return ['apples', 'oranges', 'bananas'];
       }
     }
 }]);

Html code

 <input type="text" auto-complete/>
2
  • Are you sure your service is injected correctly into your directive? Commented Mar 25, 2014 at 8:11
  • check via chrome or firebug debugger if the injected "autoCompleteDataService" has the functions you defined in your service. or use a console.log(autoCompleteDataService) Commented Mar 25, 2014 at 8:22

1 Answer 1

1

you have couple of syntax mistakes, some missing braces, naming and lastly, you forgot to surround the element with JQuery, in any case here's a working example using JQuery ui autocomplete .

Example:

app.factory('autoCompleteDataService', [function() {
    return {
        getSource: function() {

        return ['apples', 'oranges', 'bananas'];
       }
     }
 }]);

 app.directive('autoComplete', function(autoCompleteDataService) {
    return {
            restrict: 'A',
           link: function(scope, elem, attr, ctrl) {

        $(elem).autocomplete({
            source: autoCompleteDataService.getSource(), 
            minLength: 2
        });
    }
    }})

Live example: http://jsfiddle.net/choroshin/gKcMP/

Update:

Working example with JQuery autocomplete plugin (only works with jquery version <1.9)

var app=angular.module('App', []);

app.factory('autoCompleteDataService', [function() {
    return {
        getSource: function() {

        return ['apples', 'oranges', 'bananas'];
       }
     }
 }]);

 app.directive('autoComplete', function(autoCompleteDataService) {
    return {
            restrict: 'A',
           link: function(scope, elem, attr, ctrl) {

        $(elem).autocompleteArray(autoCompleteDataService.getSource(),{
             minLength: 2
        });
    }
    }})

Live Example: http://jsfiddle.net/choroshin/gKcMP/2/

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

5 Comments

@Alex.its not working for me..i tried to console.log(autoCompleteDataService) in firebug console it gives me Reference Error: autoCompleteDataService is not defined.Can you tell me how to correct this?
as I said you have some missing braces in your directive, have you tried looking at my live example?
yes..you are using jquery-ui.js and i am using jquery.autocomplete.js . so elem.autocompleteArray is correct synatx as per this..
MinLength did not work..and i tried minChars this also did not work
this is a plugin problem and not angular.

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.