2015-11-24 55 views
0
不工作

目前我在做什麼是這樣的HTML,自動選項卡中離子

<label class="item item-input"> 
    <input type="text" maxlength="1" ng-model="user.name" maxlength="1" input-move-next-on-maxlength></input> 
</label> 

<label class="item item-input"> 
    <input type="text" maxlength="1" ng-model="user.email" maxlength="1" input-move-next-on-maxlength></input> 
</label> 

和.js文件看起來像這樣

.directive("inputMoveNextOnMaxlength", function() { 
return { 
    restrict: "A", 
    link: function($scope, element) { 
     element.on("label", function(e) { 
      if(element.val().length == element.attr("maxlength")) { 
       var $nextElement = element.next(); 
       if($nextElement.length) { 
        $nextElement[0].focus(); 
       } 
      } 
     }); 
    } 
} 

})

但是,結果是自動標籤不工作。我也嘗試刪除<label>,工作。我想知道是否因爲輸入是在其他類或我的代碼是否有錯誤而發生。

+0

長度是多少? –

+0

是的,它工作時,我刪除了這個'

回答

0

嘗試用這種解決方案,它爲我
HTML:

<div ng-app="autofocus"> 
    <label>Name:</label> 
    <input ng-model="maxLengthReach"></input> 
    <br/><br/> 
    <label>Title:</label> 
    <input autofocus-when></input> 
</div> 

的Javascript:
VAR應用= angular.module( '自動對焦',[]);

app.directive('autofocusWhen', function() { 
    return function (scope, element, attrs) { 
     scope.$watch('maxLengthReach', function(newValue){ 
      if (newValue.length >= 5) {             
       element[0].focus(); 
      } 
     }); 
    } 
}); 

調整根據您的要求,要實現在移動設備中的自動標籤

+0

感謝它的工作,但問題是當我刪除這個'

+0

非常感謝你,我已經修復了我的代碼,它的功能就像一個魅力。我只是在它上面添加新的樣式顏色邊框。 :d –