2015-10-20 37 views
0

我想爲電子郵件地址構建一個多輸入區域。我開始建設有純angularjs的原型,即工作:Ionic focus從input-emelent中刪除

angular-fiddle

輸入字段被顯示,如果一個元素獲得點擊:

$scope.focus = true; 

這是我需要顯示的變量輸入字段。如果焦點爲假,則不顯示輸入字段。或者如果該區域收到點擊事件。

爲了將焦點設置上的投入,以便開始直接輸入,我用:

element[0].focus(); 

這工作,除非我用的是離子的框架。一旦輸入了關注的焦點,它帶走:

ionic-fiddle

爲什麼用離子所採取的重點了嗎?

回答

1

autofocus似乎是問題所在。它會立即啓動模糊事件並導致再次隱藏輸入字段。在輸入可見之後嘗試手動將焦點放在輸入字段上(使用$ timeout)。見http://jsfiddle.net/awqtsLpy/。我將edit函數移動到了directive.link部分以訪問該元素。

app.directive('emailRecipients', function ($timeout) { 
    return { 
     restrict: 'E', 
     controller: function ($scope) { 
      $scope.focus = false; 
      $scope.addressees = []; 
      var i = 10; 
      while (i) { 
       $scope.addressees.push({ 
        "name": i 
       }); 
       i--; 
      } 
     }, 
     link: function ($scope, element) { 
      $scope.click = function() { 
       $scope.focus = !$scope.focus; 
       $scope.value = null; 
      } 
      $scope.deleteFromInput = function ($event) { 

       $event.stopPropagation(); 
      } 
      $scope.edit = function ($event, addressee) { 
       $event.stopPropagation(); 
       $scope.value = addressee; 
       addressee.editable = true; 
       $scope.focus = true; 
       $timeout(function() { 
        element.find('input').focus(); 
       }); 
      } 
     } 
    } 
}); 
+0

$ timeout也適用於我的版本。我試過了scope。$ apply和setTimeout之前,這些都不起作用。 :) – marcel