2015-08-25 40 views
0

我想每次點擊一個輸入元素時切換div的可見性。我有以下HTML:ngShow從移動Safari上的輸入獲取焦點

<input type="text" ng-click="show = !show"/> 
<div ng-show="show">Im visible</div>  

在移動Safari(iPad)輸入失去焦點時div顯示。因此無法在輸入中輸入文字。有時鍵盤會彈出一半,但會回落。

我試圖通過每次div變爲可見時調用焦點函數來再次對焦輸入。下面是該指令碼(簡體):

app.directive('myDir', function(){ 
    return{ 
     link: function(scope, element, attr){ 
      scope.focus = function(){ 
       setTimeout(function() { 
        element.find('input').eq(0)[0].focus(); 
       }, 500); 
      } 
     }, 
     template: '<input type="text" ng-click="toggle()"/>' + 
        '<div ng-show="show">Im visible</div>', 
     controller: function($scope){ 
      $scope.toggle = function(){ 
       $scope.show = !$scope.show; 
       if($scope.show) $scope.focus(); 
      }; 
     } 
    }; 
}; 

這就對輸入焦點,但鍵盤顯示不出來,所以還是挺有用的。

它可以在Android設備和桌面上正常工作。

有誰知道如何解決/解決這個問題?

回答

0

我爲該指令創建了一個運動員http://plnkr.co/edit/lqrtCO8KuPtA3Cge8Qxr。 一切似乎都在iOS8上工作正常,一旦我作出了指令數的變化(包裹模板中含有股利和轉換的setTimeout對角的$超時)

app.directive('myDir', ['$timeout', function($timeout){ 
    return { 
    restrict: 'EA', 
    link: function(scope, element, attr){ 
     scope.focus = function(){ 
      $timeout(function() { 
       element.find('input').eq(0)[0].focus(); 
      }); 
     } 
    }, 
    template: '<div><input type="text" ng-click="toggle()"/>' + 
       '<div ng-show="show">Im visible</div></div>', 
    controller: function($scope){ 
     $scope.toggle = function(){ 
      $scope.show = !$scope.show; 
      if($scope.show) { 
       $scope.focus(); 
      } 
     }; 
    } 
    }; 
}]); 

一切應處於工作狀態,雖然我曾經聽到過iOS側重於未被「點擊」處理程序處理的輸入問題。但是,您的指令直接點擊輸入!

相關問題