Angular按钮单击模板参考变量不起作用
在角度应用中,我有两个HTML按钮。不确定是什么引起问题。
第一个按钮如下:我有按钮单击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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获得具有
#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.

Here is the documentation for
@ViewChild