2015-10-20 62 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