【问题标题】:input.setfocus is not a function in Ionic and Angularinput.setfocus 不是 Ionic 和 Angular 中的函数
【发布时间】:2018-08-22 13:09:26
【问题描述】:

单击某个字段时,我希望它出现在我的移动键盘上。为此,我尝试使用以下代码,但出现错误。

Home.html代码如下:

  <div *ngIf="this.primary">
    <textarea style="background-color:#f5f8fa;border:none;width:80%;margin-left:25px;" autosize #input  (click)="focusInput(input)" (input)= "charvl()" placeholder="Enter description.." name="taskdesc" [(ngModel)]="taskdesc"></textarea>
  </div>

Home.ts代码如下:

  focusInput(input) 
  {
    setTimeout(() => {
      input.setFocus();
    },150);


    this.content.scrollToBottom(300);
    console.log("focus");
    }

当我单击该字段时,我在控制台上看到此错误。

input.setfocus 不是函数

【问题讨论】:

标签: angular ionic-framework ionic2 ionic3


【解决方案1】:

试试@ViewChild

import { Component, ViewChild } from '@angular/core';

export class PageName {

  @ViewChild('input') myInput;
  constructor(public navCtrl: NavController) {}

    focusInput()  // no need to pass the input as parameter we are accessing the html element using @ViewChild
      setTimeout(() => {
      this.myInput.setFocus();
    },150);

    this.content.scrollToBottom(300);
    console.log("focus");
  }  
}

【讨论】:

  • 我使用的是 textarea 而不是 ion-textarea 。它起作用了,但是当我在ion-searchbar 上尝试相同的事情时,没有错误,但它没有出现
  • 是的,我错过了,你需要使用 ion-textarea,我还没有将它用于搜索栏,但尝试使用这个 @ViewChild('searchbar') 搜索栏:并从 'ionic-angular' 导入 { Searchbar };
猜你喜欢
  • 1970-01-01
  • 2019-11-12
  • 2021-01-20
  • 2022-12-05
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多