【问题标题】:How to make 'Outside' print to the console only after printing 'Yes'/'No' in Angular 10如何仅在 Angular 10 中打印“是”/“否”后将“外部”打印到控制台
【发布时间】:2022-01-27 04:07:39
【问题描述】:

app.component.html 包含一个按钮,单击该按钮会在 openConfirmationDialog 方法下执行代码。 在此方法中,我首先打印出外部,然后仅基于用户从对话框中选择的“是”或“否”打印在控制台上。

如何在“外部”之前将“是”/“否”打印到控制台? Output image

app.component.html:

<button class="btn btn-primary" (click)="openConfirmationDialog()">Click</button>

app.component.ts:

import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { Component } from '@angular/core';
import { ConfirmationDialogComponent } from './confirmation-dialog/confirmation-dialog.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  constructor(private dialog: MatDialog){

  }
  openConfirmationDialog(){
    this.dialog.open(ConfirmationDialogComponent, {
      disableClose: false
    }).afterClosed().subscribe(
      (data: any) => {
        if(data){
          console.log('yes');
        }
        else{
          console.log('no');
        }
      }
    );

    console.log('Outside');
}

}

confirmation-dialog.component.html:

<h1 md-dialog-title>Confirm</h1>
<div md-dialog-content>{{confirmMessage}}</div>
<div md-dialog-actions>
  <button md-button style="color: #fff;background-color: #153961;" (click)="dialogRef.close(true)">Confirm</button>
  <button md-button (click)="dialogRef.close(false)">Cancel</button>
</div>

confirmation-dialog.component.ts

import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { Component, Input, OnInit } from '@angular/core';

@Component({
  selector: 'confirm-dialog',
  templateUrl: './confirmation-dialog.component.html',
})
export class ConfirmationDialogComponent implements OnInit{
  constructor(public dialogRef: MatDialogRef<ConfirmationDialogComponent>) {}

  ngOnInit(){

  }

  public confirmMessage:string='';
}

【问题讨论】:

  • 我能知道您为什么要在Yes/No 之后显示Outside 的原因吗,知道用例/流程将帮助我以更好的方式回答它。
  • @HassanMoin 我希望流程逐行执行。在这种情况下,“是/否”在“外部”之前。
  • 请参考我贴的答案,不能逐行运行,因为你使用的函数本质上是async。
  • @HassanMoin 有效。但是除了finalize之外还有其他解决方案吗?例如,如果有 100 行代替 console.log('Outside') 并且我希望只有在 subscribe 内的所有行都执行后才执行这 100 行。我不能将所有这 100 行放在 finalize 中。还有其他解决方案吗?
  • 您可以使用这 100 行代码创建一个单独的函数,然后在 finalize 中调用该函数来为您完成这项工作。 finalize(()=&gt; this.myHugeFunction( ) )

标签: html angular typescript


【解决方案1】:

您可以使用 RxJs 运算符 finalize 在 Yes 或 No 之后运行您的代码 操作员 finalize 将在 observable 完成或错误后调用您的函数

您的代码将如下所示:

openConfirmationDialog(): void {
        this.dialog.open(ConfirmationDialogComponent, { disableClose: false })
            .afterClosed()
            .pipe(finalize(() => console.log('Outside')))
            .subscribe((data: any) => {
                if (data) {
                    console.log('yes');
                } else {
                    console.log('no');
                }
            });
        console.log('Not Async so it will always run before Yes or No');
    }

注意subscription 之外的console.log 将始终在yes、no 或Outside 之前运行,因为subscribe 代码块是async

输出会是这样的:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2014-08-28
    • 2011-01-30
    相关资源
    最近更新 更多