我想按Enter键或登录按钮提交。提交时仅单击按钮即可。 谁能帮助我让他们两个都工作?
<div class="BLog">
<div class="Log" (click)="Log()">
Log in
</div>
</div>
async Log() {
let self = this;
let ident = {
username: self.username,
password: self.password,
client_id: "task",
grant_type: "password",
}
let aux = await self.logService.postAt(ident);
if(aux) self.router.navigate(['first']);
}
答案 0 :(得分:3)
您必须将其连接成表格,并添加类型为button
的提交。
<form (submit)="Log()">
<div class="BLog">
<div class="Log">
<button type="submit">Log in</button>
</div>
</div>
</form>
答案 1 :(得分:0)
如果您想不使用表格来做, https://stackblitz.com/edit/angular-4k2xr4
export class AppComponent {
name = 'Angular';
Log() {
this.name = "Logged In"
}
handleKeyboardEvent(event) {
if (event.keyCode === 13) {
this.Log()
}
}
}
<hello name="{{ name }}"></hello>
<div class="BLog" (keydown)="handleKeyboardEvent($event)">
<input value="type and press enter">
<div class="Log">
Log in
</div>
</div>