Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度4:Viewchild设置焦点_Angular_Typescript_Viewchild - Fatal编程技术网

Angular 角度4:Viewchild设置焦点

Angular 角度4:Viewchild设置焦点,angular,typescript,viewchild,Angular,Typescript,Viewchild,我试图在不使用标记autofocus=true的情况下控制按钮焦点,但这似乎无法正常工作,您认为有必要安装管道吗 <button #btn_focus> @ViewChild('btn_focus') firstNameElement: ElementRef; ngAfterViewInit(){ this.firstNameElement.nativeElement.focus(); } @ViewChild('btn_focus')firstNameE

我试图在不使用标记autofocus=true的情况下控制按钮焦点,但这似乎无法正常工作,您认为有必要安装管道吗

 <button #btn_focus>

 @ViewChild('btn_focus') firstNameElement: ElementRef;

  ngAfterViewInit(){
    this.firstNameElement.nativeElement.focus();
  }

@ViewChild('btn_focus')firstNameElement:ElementRef;
ngAfterViewInit(){
this.firstNameElement.nativeElement.focus();
}

谢谢。

我也有同样的问题,尝试了多种方法,但都没有成功。 最后,我使用javascript代码关注具有属性id的给定HTML元素

function focusInputelement(id) {
        window.setTimeout(() => {
              document.getElementById(id).focus();
        }, 500);
    }

您的代码正在运行,您可以在此处看到它的运行:


我打赌你的问题是刚刚打开的开发者控制台,它在页面加载后立即窃取焦点。尝试在开发者控制台关闭的情况下打开stackblitz(工作),然后打开控制台(并使其聚焦,使光标在那里闪烁)并刷新页面(不工作)

您的代码正在运行,请在此处查看:非常感谢,我非常感谢,但从我的角度来看,我在NgtableComponent.html:38错误类型错误:无法读取undefined的属性“nativeElement”,可能是因为我使用的是模式页面?没有代码很难判断。试着在stackblitz中重现这个错误