вы можете использовать диалог подтверждения материала для того же. Пожалуйста, найдите ниже код:
- Я открываю здесь мое диалоговое окно подтверждения от app.component.ts, но вы можете открыть его с вашего registration.component.ts. вот код app.component.ts
import { Component, Inject } from '@angular/core';
import { VERSION, MatDialogRef, MatDialog, MAT_DIALOG_DATA } from '@angular/material';
import {ConfirmationDialog} from './confirmation-dialog.component';
@Component({
selector: 'material-app',
template: ` <p>
<button mat-button (click)="openDialog('required data to be shown on dialog')">Open Confirm Dialog</button>
</p>`
})
export class AppComponent {
version = VERSION;
constructor(private dialog: MatDialog) {
}
openDialog(form_data) {
const dialogRef = this.dialog.open(ConfirmationDialog,{
data:{
message: 'Are you sure want to submit?',
formData : form_data,
buttonText: {
ok: 'submit and continue',
cancel: 'Edit Details'
}
}
});
dialogRef.afterClosed().subscribe((confirmed: boolean) => {
// any actions to be performed on close if required
});
}
}
добавить требуемый импорт в app.module.ts:
import {BrowserModule} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {
MatAutocompleteModule,
MatButtonModule,
MatButtonToggleModule,
MatDialogModule,
} from '@angular/material';
import {AppComponent} from './app.component';
import {ConfirmationDialog} from './confirmation-dialog.component';
/**
* NgModule that includes all Material modules that are required to serve
* the Plunker.
*/
@NgModule({
exports: [
MatButtonModule,
MatButtonToggleModule,
MatDialogModule,
]
})
export class MaterialModule {}
@NgModule({
imports: [
BrowserModule,
CommonModule,
MaterialModule,
FormsModule,
ReactiveFormsModule,
BrowserAnimationsModule
],
declarations: [AppComponent,ConfirmationDialog ],
entryComponents: [ConfirmationDialog ],
bootstrap: [AppComponent],
providers: []
})
export class AppModule {}
Создать новый компонент для отображения диалога. не забудьте добавить этот компонент в
entryComponents app.module.ts.:
import { Component, Inject } from '@angular/core';
import { VERSION, MatDialogRef, MatDialog, MatSnackBar, MAT_DIALOG_DATA } from '@angular/material';
@Component({
selector: 'confirmation-dialog',
template: `<mat-dialog-content>
<p>
Display your formdata here:{{formData}}
</p>
<p>
{{message}}
</p>
</mat-dialog-content>
<mat-dialog-actions align="center">
<button mat-raised-button color="primary" (click)="onConfirmClick()" tabindex="1">{{confirmButtonText}}</button>
<button mat-raised-button mat-dialog-close tabindex="-1">{{cancelButtonText}}</button>
</mat-dialog-actions>`,
})
export class ConfirmationDialog {
message: string = "Are you sure?"
formData : any;
confirmButtonText = "Yes"
cancelButtonText = "Cancel"
constructor(
@Inject(MAT_DIALOG_DATA) private data: any,
private dialogRef: MatDialogRef<ConfirmationDialog>) {
if(data){
this.formData = data.formData;
this.message = data.message || this.message;
if (data.buttonText) {
this.confirmButtonText = data.buttonText.ok || this.confirmButtonText;
this.cancelButtonText = data.buttonText.cancel || this.cancelButtonText;
}
}
}
onConfirmClick(): void {
this.dialogRef.close(true);
// do further actions as per your requirement such as redirection or something else.
}
}