(function () {
  angular
    .module("ng-geocoder")
    .directive("ngGeocoder", ngGeocoder);

  ngGeocoder.$inject = ["$timeout", "ngGeocoderService"];

  function ngGeocoder ($timeout, ngGeocoderService) {
    var directive = {
      "restrict": "AE",
      "scope": {
        "result": "=ngGeocoder",
        "id": "=",
        "wait": "=",
        "minLength": "=",
        "placeholder": "@",
        "textSearching": "@",
        "textNoResults": "@"
      },
      "controller": ngGeocoderCtrl,
      "controllerAs": "vm",
      "bindToController": true,
      "link": linkFunction,
      "template": INCLUDE_FILE("ng-geocoder.html")
    };

    return directive;

    function ngGeocoderCtrl () {
      var vm = this;

      vm.index   = 0;
      vm.results = [];

      vm.select        = select;
      vm.displayList   = displayList;
      vm.inputKeypress = inputKeypress;

      function select (index) {
        vm.result = vm.results[index];
      }

      function inputKeypress ($event) {
        if (event.keyCode === 38 || event.keyCode === 40) return handleArrowKeys($event.keyCode);
        if (event.keyCode === 13)                         return select(vm.index);
      }

      function handleArrowKeys(key) {
        var index         = vm.index;
        var resultsLength = vm.results.length;

        var arrowUp   = key === 38;
        var arrowDown = key === 40;

        if (arrowUp) {
          index = index > 0 ? index - 1 : resultsLength;
        }

        if (arrowDown) {
          index = index < resultsLength ? index + 1 : 0;
        }

        vm.index = index;
      }

      function displayList () {
        return vm.inputFocused || vm.results.length;
      }
    }

    function linkFunction (scope, element, attributes) {
      var waitTimeout;

      var $el   = element[0];
      var input = $el.querySelector(".ng-geocoder__input");

      input.addEventListener("input",    inputChanged);

      function inputChanged (event) {
        var length = input.value.length;

        if (length <= scope.vm.minLength || scope.vm.isSearching) { return; }

        if (waitTimeout) { $timeout.cancel(waitTimeout); }

        waitTimeout = $timeout(search, scope.vm.wait || 500);
      }

      function search (query) {
        scope.vm.isSearching = true;

        return ngGeocoderService.geocodeByQuery(query || input.value)
          .then(geocodeSuccess);
      }

      function geocodeSuccess (results) {
        scope.vm.isSearching = false;
        scope.vm.results     = results || [];
      }
    }
  }
})();

@@ -355,32 +355,59 @@
 inLength%22: %22=%22,%0A
+        %22inputClass%22: %22@%22,%0A
         %22placeho
@@ -764,16 +764,17 @@
 index   
+ 
 = 0;%0A   
@@ -791,13 +791,41 @@
 lts 
+ 
 = %5B%5D;
+%0A      vm.showList = false;
 %0A%0A  
@@ -844,17 +844,16 @@
 t       
- 
 = select
@@ -876,17 +876,16 @@
 ayList  
- 
 = displa
@@ -908,21 +908,20 @@
 inputKey
-press
+down
  = input
@@ -923,21 +923,20 @@
 inputKey
-press
+down
 ;%0A%0A     
@@ -980,16 +980,18 @@
 .result 
+  
 = vm.res
@@ -1003,16 +1003,45 @@
 index%5D;%0A
+        vm.showList = false;%0A
       %7D%0A
@@ -1068,13 +1068,12 @@
 tKey
-press
+down
  ($e
@@ -1084,32 +1084,149 @@
 ) %7B%0A        if (
+!$event %7C%7C !$event.keyCode) return;%0A%0A        var enterPressed  = $event.keyCode === 13;%0A        var arrowsPressed = $
 event.keyCode ==
@@ -1233,16 +1233,17 @@
 = 38 %7C%7C 
+$
 event.ke
@@ -1254,24 +1254,92 @@
 e === 40
-) return
+;%0A%0A        if (enterPressed %7C%7C arrowsPressed) %7B%0A          if (arrowsPressed)
  handleA
@@ -1376,82 +1376,128 @@
     
+  
 if (e
-vent.keyCode === 13)                         return select(vm.index);
+nterPressed)  select(vm.index);%0A%0A          $event.preventDefault();%0A          $event.stopPropagation();%0A        %7D
 %0A   
@@ -1741,48 +1741,10 @@
 ndex
- = index %3E 0 ? index - 1 : resultsLength
+--
 ;%0A  
@@ -1796,18 +1796,43 @@
 ndex
- = 
+++;%0A        %7D%0A%0A        if (
 index 
-%3C
+%3E=
  res
@@ -1845,33 +1845,32 @@
 ngth
- ?
+) %7B
  index 
-+ 1 :
+=
  0;
+ %7D
 %0A       
  %7D%0A%0A
@@ -1857,32 +1857,76 @@
 x = 0; %7D%0A       
+ if (index %3C 0) %7B index = resultsLength - 1;
  %7D%0A%0A        vm.i
@@ -1995,16 +1995,17 @@
  return 
+(
 vm.input
@@ -2032,16 +2032,32 @@
 s.length
+) && vm.showList
 ;%0A      
@@ -2274,19 +2274,16 @@
 %22input%22,
-   
  inputCh
@@ -2609,24 +2609,59 @@
 h (query) %7B%0A
+        scope.vm.results     = %5B%5D;%0A
         scop
@@ -2920,24 +2920,61 @@
 ults %7C%7C %5B%5D;%0A
+        scope.vm.showList    = true;%0A
       %7D%0A    

Fix arrows behavior, clear results on change and fix list display