Angular按钮单击模板参考变量不起作用

发布于 2025-01-24 02:14:06 字数 811 浏览 2 评论 0 原文

在角度应用中,我有两个HTML按钮。不确定是什么引起问题。

stackblitz代码

第一个按钮如下:我有按钮单击1st按钮,带有模板参考变量

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

第二个按钮如下: 问题:我有焦点事件事件,并希望将重点放在第二个按钮上时,将焦点设置为第1按钮。但是由于某种原因,模板参考变量是将错误设置为第一个按钮。

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

打字稿文件:

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

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

I have two html buttons in angular application. Not sure what is causing issue.

Stackblitz code here

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. 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:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2025-01-31 02:14:06

获得具有 #elementid 的HTML元素

时是。

@ViewChild('B',{static:false})elonno:elementRef

在您的stackblitz示例中,一些称为的方法似乎丢失了。

这是文档代码>

When getting an HTML element that has #elementId you'll have to use

@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.
enter image description here

Here is the documentation for @ViewChild

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文