Last active
April 3, 2023 16:35
-
-
Save jhades/a3770850f74893670f4610b6db655733 to your computer and use it in GitHub Desktop.
Angular Material Dialog - https://blog.angular-university.io/angular-material-dialog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {MatDialogModule} from "@angular/material"; | |
| @NgModule({ | |
| declarations: [ | |
| ... | |
| CourseDialogComponent | |
| ], | |
| imports: [ | |
| ... | |
| MatDialogModule | |
| ], | |
| providers: [ | |
| ... | |
| ], | |
| bootstrap: [AppComponent], | |
| entryComponents: [CourseDialogComponent] | |
| }) | |
| export class AppModule { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {MatDialog, MatDialogConfig} from "@angular/material"; | |
| @Component({ | |
| selector: 'app-root', | |
| templateUrl: './app.component.html', | |
| styleUrls: ['./app.component.css'] | |
| }) | |
| export class AppComponent { | |
| constructor(private dialog: MatDialog) {} | |
| openDialog() { | |
| const dialogConfig = new MatDialogConfig(); | |
| dialogConfig.disableClose = true; | |
| dialogConfig.autoFocus = true; | |
| this.dialog.open(CourseDialogComponent, dialogConfig); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dialogConfig.position = { | |
| 'top': '0', | |
| left: '0' | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <h2 mat-dialog-title>{{description}}</h2> | |
| <mat-dialog-content [formGroup]="form"> | |
| <mat-form-field> | |
| <input matInput | |
| placeholder="Course Description" | |
| formControlName="description"> | |
| </mat-form-field> | |
| .... | |
| </mat-dialog-content> | |
| <mat-dialog-actions> | |
| <button class="mat-raised-button"(click)="close()">Close</button> | |
| <button class="mat-raised-button mat-primary"(click)="save()">Save</button> | |
| </mat-dialog-actions> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| openDialog() { | |
| const dialogConfig = new MatDialogConfig(); | |
| dialogConfig.disableClose = true; | |
| dialogConfig.autoFocus = true; | |
| dialogConfig.data = { | |
| id: 1, | |
| title: 'Angular For Beginners' | |
| }; | |
| this.dialog.open(CourseDialogComponent, dialogConfig); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material"; | |
| @Component({ | |
| selector: 'course-dialog', | |
| templateUrl: './course-dialog.component.html', | |
| styleUrls: ['./course-dialog.component.css'] | |
| }) | |
| export class CourseDialogComponent implements OnInit { | |
| form: FormGroup; | |
| description:string; | |
| constructor( | |
| private fb: FormBuilder, | |
| private dialogRef: MatDialogRef<CourseDialogComponent>, | |
| @Inject(MAT_DIALOG_DATA) data) { | |
| this.description = data.description; | |
| } | |
| ngOnInit() { | |
| this.form = fb.group({ | |
| description: [description, []], | |
| ... | |
| }); | |
| } | |
| save() { | |
| this.dialogRef.close(this.form.value); | |
| } | |
| close() { | |
| this.dialogRef.close(); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| close() { | |
| this.dialogRef.close(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| save() { | |
| this.dialogRef.close(this.form.value); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| openDialog() { | |
| const dialogConfig = new MatDialogConfig(); | |
| dialogConfig.disableClose = true; | |
| dialogConfig.autoFocus = true; | |
| dialogConfig.data = { | |
| id: 1, | |
| title: 'Angular For Beginners' | |
| }; | |
| this.dialog.open(CourseDialogComponent, dialogConfig); | |
| const dialogRef = this.dialog.open(CourseDialogComponent, dialogConfig); | |
| dialogRef.afterClosed().subscribe( | |
| data => console.log("Dialog output:", data) | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment