可以使用@ViewChild將焦點設定在特定的文字區域嗎?
P粉477369269
P粉477369269 2023-09-08 16:58:05
0
2
297

我有一個前端角度應用程序,我希望將焦點設置到特定的文本區域,遊標閃爍並準備好讓用戶在加載時在文本區域中鍵入。

在進行了一些谷歌搜尋後,我認為 @ViewChild 可能是正確的選擇。但到目前為止我已經能夠讓它工作了。

這是我的整個獨立 ts 檔案:

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


@Component({
  selector: 'my-app',
  template: `
    <h1>Hello from {{name}}!</h1>
    <textarea #reference placeholder="Start typing"></textarea>
  `
})
export class App {

  @ViewChild('reference') textarea: ElementRef<HTMLTextAreaElement> | undefined;
  name = 'Angular';

  ngAfterViewInit(): void {
    this.textarea?.nativeElement.focus();
  }

}

P粉477369269
P粉477369269

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!