It seems that the template mechanism causes typeahead.js to expect an html string, rather than a dom element (in dropdown_view.js). So I'm not sure if there is a good solution for using the angular template. As a test, I managed to bind the result to the angular template, but it should render the element and then get the html value from the element after data binding. I do not like this approach, but I decided that someone might find it useful. I think it will go with the template function, as in the previous post.
Jade template looks like
.result p {{datum.tokens}} p {{datum.value}}
Directive
angular.module('app').directive('typeahead', [ '$rootScope', '$compile', '$templateCache', function ($rootScope, $compile, $templateCache) { // get template from cache or you can load it from the server var template = $templateCache.get('templates/app/typeahead.html'); var compileFn = $compile(template); var templateFn = function (datum) { var newScope = $rootScope.$new(); newScope.datum = datum; var element = compileFn(newScope); newScope.$apply(); var html = element.html(); newScope.$destroy(); return html; }; return { restrict: 'A', link: function (scope, element, attrs, ctrl) { element.typeahead({ name: 'server', remote: '/api/search?q=%QUERY', template: templateFn }); element.on('$destroy', function () { element.typeahead('destroy'); }); element.on('typeahead:selected', function () { element.typeahead('setQuery', ''); }); } }; } ]);
craigm
source share