Skip to content

Instantly share code, notes, and snippets.

@borntorun
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borntorun/04eddcf5edd0abdb50d9 to your computer and use it in GitHub Desktop.
Save borntorun/04eddcf5edd0abdb50d9 to your computer and use it in GitHub Desktop.
<div ng-controller="RemotePrefetchCtrl as vm">
<div class="scrolling">Item scrolled:&nbsp;{{vm.itemonCursorChanged}}</div>
<div class="selection">Selection:&nbsp;<span ng-repeat="item in vm.itemonSelected">{{item}}</span></div>
<angular-typeaheadjs angty-ttoptions="{{vm.ttoptions}}"
angty-options="{{vm.options}}"
angty-onselect="vm.onSelected"
angty-onclose="vm.onClosed"
angty-oncursorchange>
<input class="typeahead" type="text" placeholder="Search for Countries"/>
</angular-typeaheadjs>
</div>
angular.module('appdemo', ['angularTypeaheadjs'])
.controller('RemotePrefetchCtrl', [ '$scope', RemotePrefetchCtrl ]);
function RemotePrefetchCtrl($scope) {
var vm = this;
vm.itemonSelected = [];
vm.ttoptions = {
prefetch: 'data/countries.json',
remote: 'data/%QUERY.json',
name: 'countries',
display: 'name',
minLength: 1,
classNames: {input: 'inputclass'},
sufficient: 10,
limit: 5
};
vm.options = {
clear: true
}
$scope.$on('typeahead:cursorchange', function (ev, item) {
$scope.$apply(function () {
vm.itemonCursorChanged = item[1].name;
});
});
vm.onSelected = function (event, item) {
$scope.$apply(function () {
for (var i = vm.itemonSelected.length - 1; i >= 0; i--) {
if (vm.itemonSelected[i] === item.name) {
vm.itemonSelected.splice(i, 1);
}
}
vm.itemonSelected.push(item.name);
vm.itemonCursorChanged = '';
});
};
vm.onClosed = function () {
$scope.$apply(function () {
vm.itemonCursorChanged = '';
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment