【发布时间】: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(()=> this.myHugeFunction( ) )
标签: html angular typescript