angular - 我们如何在 angular7 中将焦点放在输入元素上
问题描述
我已经在 ts 文件中导入了 ViewChild、ElementRef、Renderer,但它显示错误“错误类型错误:无法读取未定义的属性‘nativeElement’”
import { Component, OnInit,ViewChild,ElementRef,Renderer } from '@angular/core';
@ViewChild('usernameField') input : ElementRef;
this.usernameField.nativeElement.focusIn();
期望专注于输入元素。
解决方案
我认为这会奏效
代替
this.usernameField.nativeElement.focusIn();
尝试
this.usernameField.nativeElement.focus();
或者发布您的 html 以便我们查看问题所在?
只需检查您的输入标签#usernameField
喜欢
<input #usernameField
并改变你的 ts
@ViewChild('usernameField') usernameField: ElementRef;
还要确保您已将焦点添加到ngAfterViewInit
喜欢
ngAfterViewInit() {
this.usernameField.nativeElement.focus();
}
推荐阅读
- three.js - Three.js:如何在鼠标悬停时将立方体变形为球体?
- java - RecyclerView 中 MapView 的 IndexOutOfBoundsException
- testng - 如何使用“dependsOnMethods”以编程方式查找触发当前测试方法的测试方法名称
- r - 操作嵌套列表中的值
- swift - 如何在 Swift UIKit Map 组件中处理 SwiftUI 中的触摸手势?
- django - DRF反向未找到正确的路线
- ionic-framework - 如何使 ion-col 在 col 上扩展文本
- python - Errno 11001 getaddrinfo 在遍历 url 时失败
- python - Graph-tool:如何访问 add_edge_list 返回的顶点值属性映射?
- ansible - SSH 错误:无法将数据发送到远程主机 \"xxxx\"。确保可以通过 ssh 访问此主机