Created
December 10, 2015 10:23
-
-
Save MagnusThor/34d27f843a0cf5abde13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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