In Angular 8 I want to implement an Angular-Material Modal-Dialog. Upon clicking on a user image, the user data should be displayed in the modal. I am unable to pass the dynamic data of the observable from the Profile component to the Profile-Modal component. For testing, I subscribed to the observable in the ngOnInit() of Profile.component.ts, the array of data objects is console-logged properly. If I use the same subscription in openDialog() which serves to open the Modal-Dialog, I get this error message: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. at NgForOf.ngDoCheck
profile.component.ts: (in profile.component.html everything works properly, *ngFor="let member of members" works correct and with interpolation I get the data, i.e. {{member.name }})
import { Component, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { MatDialog } from '@angular/material';
import { ProfileModalComponent } from '../profile-modal/profile-modal.component';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
observableData: observableData[] = [];
members: Observable<any[]>;
constructor(db: AngularFirestore,
public dialog: MatDialog) {
this.members = db.collection('members').valueChanges();
}
openDialog(): void {
let observableData = this.members.subscribe(members => members);
this.dialog.open(ProfileModalComponent, {
width: '80%',
height: '80%',
data: {data: observableData}
});
}
// logs the array with the objects
ngOnInit() {
const members = this.members.subscribe(
member => console.log(member)
)
}
}
profile-modal.component.ts:
import { Component, Inject, Input } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
@Component({
selector: 'app-profile-modal',
templateUrl: 'profile-modal.component.html',
styleUrls: ['profile-modal.component.css']
})
export class ProfileModalComponent {
constructor(public dialogRef: MatDialogRef<ProfileModalComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {}
onNoClick(): void {
this.dialogRef.close();
}
}
UPDATE
After taking a look on your stackblitz I created a fix for your problem here
https://stackblitz.com/edit/angular-vwhas1
The problem is in your *ngFor. It should be let member of data.data
.
Because you have assigned the observable like this:
data: {data: observableData}
, it means that in your modal/dialog component, you can access it by data.data
Your fixed code
<fa-icon id="closeModal" class="icon" [icon]="faTimes" (click)="onNoClick()">
</fa-icon>
<div class="container">
<h1 mat-dialog-title>Profil</h1>
<div mat-dialog-content>
<div class="member" *ngFor="let member of data.data">
<span>NAME: </span>
<span>{{member.name}}</span>
</div>
</div>
</div>