Skip to content

Instantly share code, notes, and snippets.

@MagnusThor
Created December 10, 2015 10:23
Show Gist options
  • Save MagnusThor/34d27f843a0cf5abde13 to your computer and use it in GitHub Desktop.
Save MagnusThor/34d27f843a0cf5abde13 to your computer and use it in GitHub Desktop.
return {
restrict: 'E',
scope: {
options: '=options', // arr med data?
// [{label: '' , value: 0 },... ]
select: '&onSelect',
trigger: '=trigger',
labelField: '=labelField', // label
valueField: '=valueField', // value
currentIndex: '=0'
},
link: function link(scope, element, attrs) {
var keys = { up: 38, down: 40, enter: 13 }; // key map
var index = -1;
var el = angular.element(document.querySelector("#" + attrs.trigger));
el.on("keyup", function (evt) {
switch (evt.keyCode) {
case keys.up:
index--;
break;
case keys.down:
if (index < scope.options.length-1)
index++;
break;
case keys.enter:
scope.select({ item: scope.options[index] });
break;
default:
};
scope.currentIndex = index;
});
},
templateUrl: "/app/suggestions.html"
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment