簡體   English   中英

在AngularJS中使用ui-keypress將焦點更改為下一個輸入

[英]Change focus to next input using ui-keypress in AngularJS

我在輸入字段中使用ui-keypress指令來應用函數newline(),該函數在current(focused)一個下創建一個新的輸入字段。 一切正常,但我想更新此功能,以便焦點跳至新創建的字段。

這是我的index.html:

<!doctype html>
<html ng-app="myApp">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.min.js">                                                       
    </script>
    <script src="script.js"></script>
    <script src="keypress.js"></script>
  </head>

  <body ng-controller="TextCtrl">

    <div  ng-repeat="task in tasks track by $index">
      <input type="text" class="form-control" ng-model="task.text" ui-keypress="{13:'newline($index+1)'}" >
    </div>

  </body>

</html>

這是script.js:

var myAppModule = angular.module('myApp', ['ui.keypress']);
myAppModule.controller('TextCtrl',function($scope){
var emptytask = '';
var tasks=[{text:'chart'}];
$scope.tasks = tasks;
$scope.newline = function(index){
    $scope.tasks.splice(index,0,{text:emptytask});
};
});

你可以在指令中嘗試這樣的事情嗎

.directive('focusOnLoad', function () {
    return {
     restrict: 'E',
     link: function (scope, element, attrs) {
        $timeout(function() {
          element.find('input').focus()
        },0);
     }
    }
 }

在HTML上

<div ng-repeat="task in tasks track by $index" focus-on-load>

在控制器中,只需添加新任務即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM