在Angular 8中,如何将动态数据从可观察对象传递到模态对话框?

问题描述 投票:0回答:1

在Angular 8中,我想实现一个Angular-Matalial Modal-Dialog。单击用户图像后,用户数据应以模式显示。我无法将可观察的动态数据从Profile组件传递到Profile-Modal组件。为了进行测试,我在Profile.component.ts的ngOnInit()中订阅了observable,数据对象的数组已正确控制台记录。如果在openDialog()中使用相同的订阅来打开“模态对话框”,则会收到此错误消息:找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到Iterables,例如数组。在NgForOf.ngDoCheck

profile.component.ts:(在profile.component.html中,一切正常,* ngFor =“ let member of member”正常工作,通过插值,我得到了数据,即{{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();
  }
}
angular modal-dialog observable pass-data
1个回答
0
投票

我认为您可以在收到这样的数据后打开模式:

openDialog(): void {
   this.members.subscribe(members => {
      this.dialog.open(ProfileModalComponent, {
        width: '80%',
        height: '80%',
        data: {data: members }
      });
   });
 }

尝试一下可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.