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=true
hint
-optional
,default=true
minLength
-optional
,default=3
classNames
-optional
- For typeahead dataset options (Group II)
name
-optional
display
-optional
,default='name'
limit
-optional
,default=10
- For Bloodhound options (Group III)
sufficient
-optional
,default=10
prefetch
-optional
. A URL string for prefecth data.remote
-optional
. Can be a URL string for remote suggestions or an options hash. Onlyremote.url
andremote.wildcard
(default=%QUERY
) are supported.- In the default use case (
angty-ttdatasets
is not passed) one ofremote|prefetch
must 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 theselect
event is triggered whenautocomplete
event 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:active
event -
angty-onidle
- funtion handler to thetypeahead:idle
event -
angty-onopen
- funtion handler to thetypeahead:open
event -
angty-onclose
- funtion handler to thetypeahead:close
event -
angty-onchange
- funtion handler to thetypeahead:change
event -
angty-onrender
- funtion handler to thetypeahead:render
event -
angty-onselect
- funtion handler to thetypeahead:select
event -
angty-onautocomplete
- funtion handler to thetypeahead:autocomplete
event -
angty-oncursorchange
- funtion handler to thetypeahead:cursorchange
event -
angty-onasyncrequest
- funtion handler to thetypeahead:asyncreques
t event -
angty-onasynccancel
- funtion handler to thetypeahead:asynccancel
event -
angty-onasyncreceive
- funtion handler to thetypeahead:asyncreceive
event -
angty-onbeforeactive
function handler to thetypeahead:beforeactive
event -
angty-onbeforeidle
function handler to thetypeahead:beforeidle
event -
angty-onbeforeopen
function handler to thetypeahead:beforeopen
event -
angty-onbeforeclose
function handler to thetypeahead:beforeclose
event -
angty-onbeforeautocomplete
function handler to thetypeahead:beforeautocomplete
event -
angty-onbeforeselect
function handler to thetypeahead:beforeselect
event -
angty-onbeforecursorchange
function handler to thetypeahead:beforecursorchange
event
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=false
all the typeahead events are emitted on scope. Can be catch as:
$scope.$on('typeahead:select', function() {
//do something
});
- if
emitOnlyPresent=true
only 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