我有一个像下面这样的按钮,当我按下“空格”时它会打开一个模式
<button type="button" (keydown.space)="openLg()" class="btn btn-link"></button>
我想将焦点放在下面的输入字段上
<input class="form-control" type="text" id="amount" name="amount" [(ngModel)]="totalAmt_modal">
openLg 函数是
openLg() {
this.renderer.selectRootElement('#amount').focus();
}
我也导入了 renderer2
import { Component, OnInit, Renderer2} from '@angular/core';
constructor(private renderer: Renderer2){}
但是当我按下空格按钮时,它会显示以下错误
core.js:4352 ERROR Error: The selector "#amount" did not match any elements
at EmulatedEncapsulationDomRenderer2.selectRootElement (platform-browser.js:679)
at BaseAnimationRenderer.selectRootElement (animations.js:267)
at VoucherentryComponent.setfocus (voucherentry.component.ts:347)
at VoucherentryComponent.openLg (voucherentry.component.ts:320)
at VoucherentryComponent_tr_52_Template_button_keydown_space_2_listener (voucherentry.component.html:75)
at executeListenerWithErrorHandling (core.js:15214)
at wrapListenerIn_markDirtyAndPreventDefault (core.js:15249)
at platform-browser.js:582
at platform-browser.js:1281
at ZoneDelegate.invoke (zone-evergreen.js:364)
解决办法是什么?我究竟做错了什么?