sioesi sioesi - 3 months ago 22
jQuery Question

autocomplete jquery element.autocomplete is not a function

Js import the files to my project, also try to do it from the official site url but not working.

<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>


It seems strange that this happens in all the examples I've seen of this function are identical to what I do, but the error persists for me.
I add directives and factories to work it in angularjs + ionic

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

.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
});
}
};
})

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


But I keep getting this error.

TypeError: elem.autocomplete is not a function
at link (file:///Users/lixsys/app_view/www/js/app.js:203:18)
at invokeLinkFn (file:///Users/lixsys/app_view/www/lib/ionic/js/ionic.bundle.js:16911:9)
at nodeLinkFn (file:///Users/lixsys/app_view/www/lib/ionic/js/ionic.bundle.js:16421:11)
at compositeLinkFn (file:///Users/lixsys/app_view/www/lib/ionic/js/ionic.bundle.js:15770:13)
at compositeLinkFn (file:///Users/lixsys/app_view/www/lib/ionic/js/ionic.bundle.js:15773:13)
at compositeLinkFn (file:///Users/lixsys/app_view/www/lib/ionic/js/ionic.bundle.js:15773:13)
at nodeLinkFn (file:///Users/lixsys/app_view/www/lib/ionic/js/ionic.bundle.js:16416:24)
at compositeLinkFn (file:///Users/lixsys/app_view/www/lib/ionic/js/ionic.bundle.js:15770:13)
at compositeLinkFn (file:///Users/lixsys/app_view/www/lib/ionic/js/ionic.bundle.js:15773:13)
at nodeLinkFn (file:///Users/lixsys/app_view/www/lib/ionic/js/ionic.bundle.js:16416:24) <input type="text" ng-model="foo" auto-complete="" class="ng-pristine ng-untouched ng-valid">

Answer

Please check this,

http://jsfiddle.net/swfjT/2884/

the problem is need to be invoke the controller

 <div ng-app='MyModule'>
    <div ng-controller='DefaultCtrl'>
        <input type="text" ng-model="foo" auto-complete/>
                  Foo = {{foo}}
    </div>
</div>


    angular.module('MyModule', []).controller('DefaultCtrl',['$scope', function($scope) {}])