我得到了一个像这样的被动形式
@Component({
template: `
<form [formGroup]="formGroup" (ngSubmit)="onSubmit()" novalidate>
<textarea name="detail" id="detail" formControlName="detail"></textarea>
<div *ngIf="formGroup.controls['detail'].dirty && formGroup.controls['detail'].invalid">This is required</div>
<button type="submit">Post</button>
</form>
`
})
export class CreateDiscussionComponent {
formGroup: FormGroup;
submitted: boolean = false;
constructor() {
this.formGroup = new FormGroup({
detail: new FormControl('', [Validators.required, Validators.minLength(2) ]),
type : new FormControl('discussion'),
});
}
onSubmit(): void {
this.submitted = true;
}
}
该怎么做才能通过按cmd + enter
提交表单答案 0 :(得分:4)
这应该有效:
<form (keydown.meta.Enter)="handleFormSubmit($event)">
</form>
您可以按所需的组合键过滤键盘事件
答案 1 :(得分:0)
试试这个解决方案:
@HostListener('document:keydown', ['$event'])
onKey(ev:KeyboardEvent) {
// do something meaningful with it
// check ev.key == command + enter
console.log(`The user just pressed ${ev.key}!`);
// call submit form
if (ev.ctrlKey && ev.keyCode == 13) { // cmd+enter
this.onSubmit();
}
}