【问题标题】:Enter key to move to next element in Ionic 4 Angular 8 form输入键移动到 Ionic 4 Angular 8 表单中的下一个元素
【发布时间】:2019-11-21 05:39:09
【问题描述】:

在我使用 Ionic 和 Angular 8 构建的 PWA 中,我需要处理 Enter 按键以移动到下一个元素。如何使用(keyup.enter)实现它。请帮帮我。

https://stackblitz.com/edit/ionic-3drfby

【问题讨论】:

  • 请将相关代码添加为代码块。如果(何时)链接失效,这个问题将变得完全没用。 Stackblitz 太棒了!! :) 但问题本身应该提供minimal reproducible example

标签: angular ionic3 ionic4 angular8 keyup


【解决方案1】:

我会为此编写一个自定义指令。受此answer 的启发。该指令可以放在formtag上,我们使用HostListener来监听keyup.enter,如果存在下一个元素,我们将焦点移到它。

我们需要对表单的提交方式进行一些更改,因为每次按 Enter 时都会调用 ngSubmit。所以我会从表单标签中删除它,将按钮设置为type="button",然后将点击事件附加到它以调用logForm()

所以这就是我所做的一切:

<form focusDir>
<!-- .... -->
<button ion-button type="button" block (click)="logForm()">Add Todo</button>

指令:

@Directive({
  selector: "[focusDir]"
})
export class FocusDirective {
  constructor() {}

  @HostListener("keyup.enter") onKeyupEnter() {
    var nextEl = this.findNextTabStop(document.activeElement);
    nextEl.focus();
    // or try for ionic 4:
    // nextEl.setFocus();
  }

  findNextTabStop(el) {
    var universe = document.querySelectorAll(
      "input, button, select, textarea, a[href]"
    );
    var list = Array.prototype.filter.call(universe, function(item) {
      return item.tabIndex >= "0";
    });
    var index = list.indexOf(el);
    return list[index + 1] || list[0];
  }
}

然后在 ngModule 的 declarations 数组中标记此指令。

STACKBLIZ

【讨论】:

    【解决方案2】:

    一种方法是设置下一个元素的引用。如下所示

    <ion-content padding>
     <form (ngSubmit)="logForm()">
          <ion-item>
            <ion-label>Todo1</ion-label>
            <ion-input type="text" [(ngModel)]="todo.title1" name="title1" #a (keyup.enter)="setFocus(b)"></ion-input>
          </ion-item>
          <ion-item>
            <ion-label>Todo2</ion-label>
            <ion-input type="text" [(ngModel)]="todo.title2" name="title2" #b (keyup.enter)="setFocus(c)"></ion-input>
          </ion-item>
          <ion-item>
            <ion-label>Todo3</ion-label>
            <ion-input type="text" [(ngModel)]="todo.title3" name="title3" #c (keyup.enter)="setFocus(d)"></ion-input>
          </ion-item>
          <ion-item>
            <ion-label>Description</ion-label>
            <ion-textarea [(ngModel)]="todo.description" name="description" #d></ion-textarea>
          </ion-item>
          <button ion-button type="submit" block>Add Todo</button>
        </form>
    </ion-content>
    

    .ts 文件中

    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {
    todo = {}
      logForm() {
        console.log(this.todo)
      }
    
      setFocus(nextElement) {
         nextElement.setFocus(); //For Ionic 4
        //nextElement.focus(); //older version
      }
    
      constructor(public navCtrl: NavController) {
    
      }
    
    }
    

    【讨论】:

    • 谢谢,这适用于 ion-input/textarea 等元素。当您使用基本输入元素时,即使在 ionic 4+ 中,也必须使用较旧的方式 nextElement.focus()。我通过使用 try/catch 语句解决了这个问题,当发生错误时,我调用了下一个 Element.focus();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多