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?