Injecting data to a material dialog is working well, but how can be a way to automatically update these data if they change in the parent / opener component (coming from a (WAMP) subscription for example)?
[Update]
I crated a minimal version to try out how the behavior is: https://stackblitz.com/edit/angular-njh44v?embed=1&file=src/app/app.component.ts
Open the console on the bottom right side and click on the grey box with the numbers. You see that the dialog gets the current number and does no more update after that.
[/Update]
[Update 2]
Based on the second idea of Jusmpty, this seams to work at first sight (even if the dialog shows / updates the first time after data arrive):
https://stackblitz.com/edit/angular-thd34f?embed=1&file=src/app/app.component.ts
[/Update]
The concrete case looks like this:
- There is an area component that subscribes to a WAMP service and gets all the data for the including parts. This draws all the parts with an *ngFor.
File area.component.ts - Each part has an own component showing some data. If the data in the subscriptions change, the view is correctly and automatically updated, for each / all parts.
Files part.component.ts and part.component.html - On each part a click opens a dialog where more data are showing.
Files part-details-dialog.component.ts and part-details-dialog.component.html
And on this last action / step occurs the issue where the current data are showing (injecting) but no more updated if something in the subscribed data changes.
area.component.ts
@Component({
selector: 'app-area',
template: '<app-part *ngFor="let part of plan" [partData]="part"></app-part>'
})
export class AreaComponent {
plan = [];
planasync$ = this.wampService
.topic('sendplan')
.subscribe(
res => {
let tm = new TableMap();
this.plan = tm.tableToMap(res.argskw).filter((m) => m.area === 1);
},
err => console.log("res err", err),
() => console.log("res complete"));
constructor(private wampService: WampService) { }
}
part.component.ts
import { PartDetailsDialogComponent } from './part-details-dialog/part-details-dialog.component';
@Component({
selector: 'app-part-details',
templateUrl: './part.component.html'
})
export class PartComponent {
@Input() partData: any;
constructor(public dialog: MatDialog) {}
openDetailsDialog(): void {
let dialogRef = this.dialog.open(PartDetailsDialogComponent, {
width: '650px',
height: '400px',
data: {
partData: this.partData
}
});
}
}
part.component.html
<mat-card (click)="openDetailsDialog()">
<mat-card-title>Nr: {{ partData.partNr }}</mat-card-title>
<mat-card-content>
<div>Current speed: {{ partData.speed }}</div>
</mat-card-content>
</mat-card>
part-details-dialog.component.ts
@Component({
selector: 'app-part-details-dialog',
templateUrl: './part-details-dialog.component.html'
})
export class PartDetailsDialogComponent {
constructor(public dialogRef: MatDialogRef<PartDetailsDialogComponent>, @Inject(MAT_DIALOG_DATA) public data: any) { }
onNoClick(): void {
this.dialogRef.close();
}
}
part-details-dialog.component.html
<h1 mat-dialog-title>Part Details for Nr. {{ data.partData.partNr }}</h1>
<div mat-dialog-content>
<div>Current speed details: {{ data.partData.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>