Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active April 3, 2023 16:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jhades/a3770850f74893670f4610b6db655733 to your computer and use it in GitHub Desktop.
Save jhades/a3770850f74893670f4610b6db655733 to your computer and use it in GitHub Desktop.
import {MatDialogModule} from "@angular/material";
@NgModule({
declarations: [
...
CourseDialogComponent
],
imports: [
...
MatDialogModule
],
providers: [
...
],
bootstrap: [AppComponent],
entryComponents: [CourseDialogComponent]
})
export class AppModule {
}
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);
}
}
dialogConfig.position = {
'top': '0',
left: '0'
};
<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>
openDialog() {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = {
id: 1,
title: 'Angular For Beginners'
};
this.dialog.open(CourseDialogComponent, dialogConfig);
}
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();
}
}
close() {
this.dialogRef.close();
}
save() {
this.dialogRef.close(this.form.value);
}
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