Skip to content

Instantly share code, notes, and snippets.

@failpunk
Created June 28, 2013 00:11
Show Gist options
  • Save failpunk/5881482 to your computer and use it in GitHub Desktop.
Save failpunk/5881482 to your computer and use it in GitHub Desktop.
jQuery autocomplete directive for Angular.js
var cnJqueryUi = angular.module('cn.jquery', []);
cnJqueryUi.directive('autoComplete', function($http) {
// by default do not format autocomplete data
var defaultFormatter = function(data) { return data };
return {
scope: {
source: "@", // autocomplete url to call
formatter: "=", // (optional) transform data returned before for passing to autocomplete
callBack: "=", // (optional) callback when user selects an item
},
link: function(scope, element, attrs) {
var formatter = scope.formatter || defaultFormatter
, onSelect = scope.callBack || {};
var dataSource = function(term, responseCallBack) {
$http.get(scope.source + 'term/' + term.term).then(function(response) {
responseCallBack(formatter(response.data));
});
};
// init jqueryUi autocomplete
element.autocomplete({
source: dataSource,
minLength: 2,
select: onSelect
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment