Created
October 14, 2014 14:02
-
-
Save iolo/6f5ff305577894d0db8e to your computer and use it in GitHub Desktop.
simple angular directive to support twitter's typeahead.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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