Angular Material Dialog: How To Update The Injected Data When They Change In The Parent Component?


Answer :

You can just change data of the component instance, like this:

this.dialogRef.componentInstance.data = {numbers: value}; 

Example here: https://stackblitz.com/edit/angular-dialog-update


There are 2 ways I can think of at the moment, I don't really like either, but hey...

Both ways involve sending an observable to the dialog through the data.

  1. You could pass on the observable of the part to the part component, and then pass the observable in the data to the dialog. The dialog could then subscribe to the observable and get the updates that way.

AreaComponent

@Component({   selector: 'app-area',   template: '<app-part *ngFor="let part of planAsync$ | async; i as index" [partData]="part" [part$]="part$(index)"></app-part>' }) export class AreaComponent {    plan = [];    constructor(private wampService: WampService) {   }    part$(index) {     return this.planAsync$       .map(plan => plan[index]);   }    get planAsync$() {     return this.wampService       .topic('sendplan')       .map(res => {         let tm = new TableMap();         return tm.tableToMap(res.argskw).filter((m) => m.area === 1);       });   } } 

Part Component

@Component({   selector: 'app-part-details',   templateUrl: './part.component.html' }) export class PartComponent {   @Input() partData: any;   @Input() part$    constructor(public dialog: MatDialog) {}    openDetailsDialog(): void {     let dialogRef = this.dialog.open(PartDetailsDialogComponent, {       width: '650px',       height: '400px',       data: {         partData: this.partData,         part$: this.part$       }     });   } } 

Of course the question then is how useful it is to even pass partData along, if you could just access the data directly anyway.

  1. The other way requires you to just modify the part component, but you'll have to create a subject that you feed the changes to part that the component receives. I don't really like making subjects, especially if the data is already originally coming from an observable, but whatever:

Part Component

@Component({   selector: 'app-part-details',   templateUrl: './part.component.html' }) export class PartComponent, OnChanges {   @Input() partData: any;    private Subject part$ = new Subject();    constructor(public dialog: MatDialog) {}    ngOnChanges(changes){     this.part$.next(changes['partData']);       }    openDetailsDialog(): void {     let dialogRef = this.dialog.open(PartDetailsDialogComponent, {       width: '650px',       height: '400px',       data: {         partData: this.partData,         part$: this.part$       }     });   } } 

finally, to access the data, you could change the html to

Dialog HTML

<div *ngIf="(data.part$ | async) as part">   <h1 mat-dialog-title>Part Details for Nr. {{ part.partNr }}</h1>   <div mat-dialog-content>     <div>Current speed details: {{ part.speed }}</div>   </div>   <div mat-dialog-actions>     <button mat-button (click)="onNoClick()">Cancel</button>     <button mat-button [mat-dialog-close]="data.animal" cdkFocusInitial>Ok</button>   </div> </div> 

Or you could subscribe the to observable in the dialog component, and provide the data from there


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable