Angular Ctrl+提交表格

发布于 2025-02-01 03:39:00 字数 192 浏览 3 评论 0原文

我使用模板表单时,我如何添加一个全局侦听器,该全局侦听器在所有应用程序表单中调用提交按钮

当用户按ctrl+s

,并且所有来自ngform

我都想意识到这样的事情:(没有jQuery )

$("#container form").submit();

How can i add a global listener that invoke submit button in all application forms

When the user press ctrl+s

I Use Template forms and all the froms are ngForm

I Want to realize something like this: (without jQuery)

$("#container form").submit();

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

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

发布评论

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

评论(3

云淡月浅 2025-02-08 03:39:00

您可以在全球注册以下活动。
基于您当前的哪种形式,触发提交(formubmit /触发按钮单击方法)

    document.addEventListener('keydown', e => {
      if (e.ctrlKey && e.key === 's') {
        // Stop app to open save window
        e.preventDefault();
        console.log('CTRL + S Clicked');
        // do your form submission logic here
      }
    });

You can register following event globally.
Based on what form you are currently on, trigger submitting ( formSubmit / trigger button click method )

    document.addEventListener('keydown', e => {
      if (e.ctrlKey && e.key === 's') {
        // Stop app to open save window
        e.preventDefault();
        console.log('CTRL + S Clicked');
        // do your form submission logic here
      }
    });

腻橙味 2025-02-08 03:39:00
@HostListener('document:keydown.control.s', ['$event']) onKeydownHandler(event: 
KeyboardEvent) {
   console.log('Submitted');
   event.preventDefault();
   // todo
}
@HostListener('document:keydown.control.s', ['$event']) onKeydownHandler(event: 
KeyboardEvent) {
   console.log('Submitted');
   event.preventDefault();
   // todo
}
枕头说它不想醒 2025-02-08 03:39:00

这对我有用:

@Directive({
    selector: 'form'
})
export class SaveCtrlSDirective {

    constructor(private ngForm: NgForm) { }

    @HostListener('document:keydown.control.s', ['$event'])  
    onKeydownHandler(event:KeyboardEvent) {
        event.preventDefault();
        (this.ngForm as {submitted: boolean}).submitted = true;
        this.ngForm.ngSubmit.emit(this.ngForm);
    }
}

This works for me:

@Directive({
    selector: 'form'
})
export class SaveCtrlSDirective {

    constructor(private ngForm: NgForm) { }

    @HostListener('document:keydown.control.s', ['$event'])  
    onKeydownHandler(event:KeyboardEvent) {
        event.preventDefault();
        (this.ngForm as {submitted: boolean}).submitted = true;
        this.ngForm.ngSubmit.emit(this.ngForm);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文