代码之家  ›  专栏  ›  技术社区  ›  GoGo

在角2中提交表单后导航另一页

  •  0
  • GoGo  · 技术社区  · 6 年前

    我有一个HTML表单,它捕获用户的输入并将其发送到数据库。我要做的是导航另一个页面,如详细信息页面,您可以在其中自动看到所有输入的信息。

    我的方法是将值提交到数据库,并使用传递的提交ID导航详细信息页。这是我的typescript代码:

    submit() {
      this.participantService.add(this.participant).subscribe(participant => {
      this.alertifyService.success('participant successfully added');
    }, error => {
      this.alertifyService.error('Problem occured!\n' + error);
    });
    
    this.router.navigate(['participants/participants-details', this.participant.id]);
    }
    

    但是,此代码只是将值发送到数据库。它不带我去 details 第页。实现这一目标的方法是什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Amit Chigadani    6 年前

    在路由到详细信息页之前,您不等待数据提交完成。移动 router.navigate() 在内部 subscribe's success 回拨。

    submit() {
      this.participantService.add(this.participant).subscribe(participant => {
          this.alertifyService.success('participant successfully added');
          this.router.navigate(['participants/participants-details', this.participant.id]);
      }, 
      error => {
        this.alertifyService.error('Problem occured!\n' + error);
       });
    }