Skip to content

Instantly share code, notes, and snippets.

@iolo
Created October 14, 2014 14:02
Show Gist options
  • Save iolo/6f5ff305577894d0db8e to your computer and use it in GitHub Desktop.
Save iolo/6f5ff305577894d0db8e to your computer and use it in GitHub Desktop.
simple angular directive to support twitter's typeahead.js
angular.module('io.utils', [])
module.directive('ioTypeahead', [
'$parse',
function ($parse) {
return {
restrict: "AE",
replace: true,
transclude: false,
compile: function (element, attrs) {
var modelAccessor = $parse(attrs.ioTypeahead);
return function (scope, element, attrs, controller) {
var model = modelAccessor(scope);
var obj = $(element).typeahead(model.options, model.datasets);
scope.$on('$destroy', function () {
obj.typeahead('destroy');
});
angular.forEach(model, function (v, k) {
if (typeof v === 'function' && /^typeahead:/.test(k)) {
obj.on(k, v);
}
});
};
}
};
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment