简体   繁体   English

Angular 使用模板引用变量单击按钮不起作用

[英]Angular Button click with Template reference variable is not working

I have two html buttons in angular application.我在 angular 应用程序中有两个 html 按钮。 Not sure what is causing issue.不确定是什么导致了问题。

Stackblitz code here Stackblitz 代码在这里

First button is as below: I have button click event on 1st button with template reference variable第一个按钮如下:我在第一个按钮上有按钮单击事件和模板引用变量

<button (click)="accessButton(b)" #b >Click me!</button>

Second button is as below: Problem:I have focusout event and want to set the focus to 1st button once user click on tab when the focus is on 2nd button.第二个按钮如下: 问题:我有 focusout 事件,当焦点在第二个按钮上时,一旦用户单击选项卡,我想将焦点设置到第一个按钮。 But for some reason template reference variable is getting error to set focus to first button.但由于某种原因,模板引用变量在将焦点设置到第一个按钮时出错。

<button (click)="onYes()" (focusout)="onFocusOut($event)" [disabled]="isDisabled">Yes</button>

Typescript file: Typescript 档案:

@ContentChild('onNo', { static: false }) elOnNo: ElementRef;
  accessButton(b) {
    alert('click');
  }

  onFocusOut($event) {
    this.elOnNo.nativeElement.focus();
  }

When getting an HTML element that has #elementId you'll have to use获取具有#elementId的 HTML 元素时,您必须使用

@ViewChild('elementId', {static: false}) element: ElementRef

In your case that would be.在你的情况下就是这样。

@ViewChild('b', {static: false}) elOnNo: ElementRef

Also in your StackBlitz example some methods that are called seem to be missing.同样在您的 StackBlitz 示例中,一些被调用的方法似乎丢失了。 在此处输入图像描述

Here is the documentation for @ViewChild这是@ViewChild文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM