An AngularJS directive to serve as a wrapper to the typeahead.js autocomplete library.
It allows to apply to an input field the autocomplete typeahead.js features.
In the default use case it will apply autocomplete search functionality for an input.typeahead. A dataset with a Bloddhound engine as source will be created retrieving data from prefecth and/or remote urls.
[angular.js] (https://angularjs.org/)
typeahead.js version: ~0.11.1
Q version: 1.0.1
Get and install the requirements.
- Install with Bower
$ bower install angular-typeaheadjs
- or get the javascript in dist folder: angular-typeaheadjs.js
In your html template (default and minimal use case):
<angular-typeaheadjs angty-ttoptions="..." angty-ttdatasets="..." angty-options="..." ...>
<input class="typeahead" type="text" ... />
</angular-typeaheadjs>-
angty-ttoptions- optional - options hash - mimic the typeaheadjs options - used to configure options when NOT using attributeangty-ttdatasets- For typeahead options (Group I)
highlight-optional,default=truehint-optional,default=trueminLength-optional,default=3classNames-optional
- For typeahead dataset options (Group II)
name-optionaldisplay-optional,default='name'limit-optional,default=10
- For Bloodhound options (Group III)
sufficient-optional,default=10prefetch-optional. A URL string for prefecth data.remote-optional. Can be a URL string for remote suggestions or an options hash. Onlyremote.urlandremote.wildcard(default=%QUERY) are supported.- In the default use case (
angty-ttdatasetsis not passed) one ofremote|prefetchmust be passed in
- In the default use case (
- For typeahead options (Group I)
-
angty-ttdatasets- optional - An expression that resolves to an array of typeahead datasets [*{}] to pass to typeahead.datasets (the datasets are used as is, no options(from groups II | III) from the 'angty-ttoptions' attribute are considered). When this attribute is NOT passed, an internal dataset with a Bloodhound engine as source is created for prefetch and/or remote suggestions, with group Iangty-ttoptions(or defaults) applied. * No validation is made on content of the datasets apart the type validation (is-an-array) -
angty-bhfunctions- optional - An expression that resolves to an object with functions to be passed to Bloodhound - supportted: identify/sorter -
angty-options- optional - options hash for other options for the componentuseOwnDefaults-optional,default=true. Specifying that the components default values will be used instead of typeaheadjs default ones.selectOnAutocomplete-optional,default=false. Specifying that theselectevent is triggered whenautocompleteevent occurs.clear-optional,default=true. Indicates that the value on input must be cleared on suggestion selection.emitOnlyIfPresent-optional,default=true. Indicates to only emit on scope the typeahead events that were explicity included in the html tag.setSameListenerEventBefore-optional,default=false. Indicates to set the the listener to typeahead:before the same that is set to typeahead:showLog-optional,default=false. Turn on/off the warnings and errors messages when initializing.watchInitEvent-optional,default=false. Indicates that a watch to"angtty:init:<input id|input name>"event must be set on parent scope to allow set the input value on initialization (this event will be listened only once)watchSetValEvent-optional,default=false. Indicates that a watch to"angtty:setval:<input id|input name>"event must be set on parent scope to allow set the input value
Example 1: set an autocomplete search from prefetch and remote data and overriding some options
<angular-typeaheadjs angty-options="{{vm.options}}" angty-ttoptions="{{vm.ttOptions}}" angty-bhfunctions="{{vm.bhFunctions}}">
<input class="typeahead" type="text"/>
</angular-typeaheadjs>//on the controller
var vm = this;
vm.options = {
useOwnDefaults: true,
showLog: true
};
vm.ttOptions = {
minLength: 2,
limit: 10,
prefetch: '/url/for/prefetch/data',
remote: '/url/for/remote/data'
};
vm.bhFunctions = {
identify: function(obj) {
obj.name = obj.name + '-some sufix';
return obj.name;
},
sorter: function(a,b) {
//...
}
};Example 2: sending multiple datasets with typeaheadjs defaults
<angular-typeaheadjs angty-datasets="vm.datasets">
<input class="typeahead" type="text"/>
</angular-typeaheadjs>//on the controller
var vm = this;
vm.options = {useOwnDefaults: false, clear:false};
var nbaTeams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{ "team": "Boston Celtics" },{ "team": "Dallas Mavericks" },...,{ "team": "Sacramento Kings" }]
});
var nhlTeams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [{ "team": "New Jersey Devils" },{ "team": "New York Islanders" },...,{ "team": "San Jose Sharks" }]
});
vm.ttDatasets = [{
name: 'nba-teams',
display: 'team',
source: nbaTeams,
templates: {header: '<h3 class="league-name">NBA Teams</h3>'}
},
{
name: 'nhl-teams',
display: 'team',
source: nhlTeams,
templates: {header: '<h3 class="league-name">NHL Teams</h3>'}
}];<angular-typeaheadjs angty-onactive="..." angty-onidle="..." angty-onopen="..." ... >
<input class="typeahead" type="text" ... />
</angular-typeaheadjs>-
angty-onactive- funtion handler to thetypeahead:activeevent -
angty-onidle- funtion handler to thetypeahead:idleevent -
angty-onopen- funtion handler to thetypeahead:openevent -
angty-onclose- funtion handler to thetypeahead:closeevent -
angty-onchange- funtion handler to thetypeahead:changeevent -
angty-onrender- funtion handler to thetypeahead:renderevent -
angty-onselect- funtion handler to thetypeahead:selectevent -
angty-onautocomplete- funtion handler to thetypeahead:autocompleteevent -
angty-oncursorchange- funtion handler to thetypeahead:cursorchangeevent -
angty-onasyncrequest- funtion handler to thetypeahead:asyncrequest event -
angty-onasynccancel- funtion handler to thetypeahead:asynccancelevent -
angty-onasyncreceive- funtion handler to thetypeahead:asyncreceiveevent -
angty-onbeforeactivefunction handler to thetypeahead:beforeactiveevent -
angty-onbeforeidlefunction handler to thetypeahead:beforeidleevent -
angty-onbeforeopenfunction handler to thetypeahead:beforeopenevent -
angty-onbeforeclosefunction handler to thetypeahead:beforecloseevent -
angty-onbeforeautocompletefunction handler to thetypeahead:beforeautocompleteevent -
angty-onbeforeselectfunction handler to thetypeahead:beforeselectevent -
angty-onbeforecursorchangefunction handler to thetypeahead:beforecursorchangeevent
Example:
<angular-typeaheadjs angty-onselect="vm.onselect" ...>
<input class="typeahead" type="text" ... />
</angular-typeaheadjs>//on the controller
vm.onselect = function() {
//do something
}- if callbacks are not passed and
emitOnlyIfPresent=falseall the typeahead events are emitted on scope. Can be catch as:
$scope.$on('typeahead:select', function() {
//do something
});- if
emitOnlyPresent=trueonly the ones that were explicity included as an attribute are emitted
<angular-typeaheadjs angty-onselect ...>
<input class="typeahead" type="text" ... />
</angular-typeaheadjs>$scope.$on('typeahead:select', function() {
//do something
});- This is a work in progress.
- Latest release only supports typeahead.js ~0.11.0
- Will try to follow the typeaheadjs development and releaes on new features and changes.
- Contributions and comments are welcome.
- João Carvalho
Copyright (c) 2015 João Carvalho
Licensed under the MIT License
