【问题标题】:How to get the data from a Mat angular dialog in other component?如何从其他组件的 Mat 角度对话框中获取数据?
【发布时间】:2019-12-04 12:36:06
【问题描述】:

所以我有一个组件:DetailComponent:

export class DetailComponent implements OnInit  {


  @Input() participant: ParticipantInfoDTO;

  constructor(private dialog: MatDialog, route: ActivatedRoute) {
    this.participant = route.snapshot.data['participant'];
  }

  ngOnInit() {}




  openEcheqSelectorDialog() {
    this.dialog.open(EcheqSelectorComponent, {
      width: '600px',
      maxHeight: 'calc(100vh - 2em)',
      data: {
        participant: console.log('participantIDTest', this.participant)
      }
    });
  }
}

来自:this.participant 的数据是这样的:

{participantId: "ac3d1836-2e36-4947-a62f-04cd00425bcc", email: "k@endrix.org", gender: "Male", fullName: "k kaas", firstName: "k", …}

所以我得到了正确的数据。

但现在我也想在其他组件中接收相同的数据。另一个组件如下所示:


export class EcheqSelectorComponent implements OnInit, OnDestroy {
  private subscriptions = new Subscription();
  echeqs: EcheqFamilyInfo[] = [];
  allEcheqs: EcheqFamilyInfo[] = [];
  echeqFamily: EcheqFamilyInfo;
  searchInput = '';
  filtering = false;
  participantInfo: ParticipantInfoDTO;
  echeqsToSend: EcheqFamilyInfo[] = [];
  echeqSubmissionBatchDTO: EcheqSubmissionBatchDTO;
  participantIds$: Observable<string[]>;
  patientId: string;

  public participantIds: string[] = [];

  constructor(
    private apiService: EcheqDefinitionService,
    private dialog: MatDialogRef<EcheqSelectorComponent>,
    public selectedParticipantService: SelectedParticipantsService,
    private submissionService: EcheqSubmissionMedicalService,
    private snackBar: MatSnackBar,
    @Inject(MAT_DIALOG_DATA) data: any
  ) {

  }

  ngOnInit() {
    this.subscriptions.add(
      this.apiService.listEcheqFamilies().subscribe(families => {
        this.echeqs = families;
        this.allEcheqs = families;
      })
    );

    this.subscriptions.add(
      this.selectedParticipantService.getParticipantIds
        .pipe(tap(participantIds => console.log('Participants', participantIds)))
        .subscribe()
    );

      console.log('participantId',   this.participantInfo.participantId);
  }
}

但如果我这样做:

  console.log('participantId',   this.participantInfo.participantId);

然后我会收到这个错误:

EcheqSelectorComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'participantId' of undefined
    at EcheqSelectorComponent.push../src/app/participant/modules/echeq/components/echeq-selector/echeq-selector.component.ts.EcheqSelectorComponent.ngOnInit (echeq-selector.component.ts:62)
    at checkAndUpdateDirectiveInline (core.js:18620)
    at checkAndUpdateNodeInline (core.js:19884)
    at checkAndUpdateNode (core.js:19846)
    at debugCheckAndUpdateNode (core.js:20480)
    at debugCheckDirectivesFn (core.js:20440)
    at Object.eval [as updateDirectives] (EcheqSelectorComponent_Host.ngfactory.js? [sm]:1)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:20432)
    at checkAndUpdateView (core.js:19828)
    at callViewAction (core.js:20069)

那么我必须改变什么才能接收到正确的数据?

谢谢

如果我这样做:

 public participantIds: string[] = [];

  constructor(
    private apiService: EcheqDefinitionService,
    private dialog: MatDialogRef<EcheqSelectorComponent>,
    public selectedParticipantService: SelectedParticipantsService,
    private submissionService: EcheqSubmissionMedicalService,
    private snackBar: MatSnackBar,
    @Inject(MAT_DIALOG_DATA) public data: any, public dialogRef:MatDialogRef<EcheqSelectorComponent>
  )
  {
    this.participantInfo = data.participant;
  }

  ngOnInit() {
    this.subscriptions.add(
      this.apiService.listEcheqFamilies().subscribe(families => {
        this.echeqs = families;
        this.allEcheqs = families;
      })
    );

    this.subscriptions.add(
      this.selectedParticipantService.getParticipantIds
        .pipe(tap(participantIds => console.log(' selected Participants', participantIds)))
        .subscribe()
    );

      console.log('participantId', this.dialogRef.id);
  }

但是如何获得

{participantId: "0bde3c11-50a2-4e4e-a9e3-08d6cccff86a"}

那么参与者ID?

谢谢

【问题讨论】:

    标签: javascript angular angular-material components


    【解决方案1】:

    这是一个如何从对话框接收数据的示例

    async deleteRequest(id) {
    const dialogRef = this.dialog.open(ConfirmationComponent, {
      width: '250px',
      data: 'Are you sure you want to delete this request?'
    });
     dialogRef.afterClosed().subscribe(async result => {
      if (result == "yes") {
       console.log('yes');
      }
      else{
       console.llog('No');
      }
    });
    

    在你的对话中

    export class ConfirmationComponent implements OnInit {
    
    constructor(@Inject(MAT_DIALOG_DATA) public data: any, public dialogRef: 
    MatDialogRef<ConfirmationComponent>) { }
    
    onNoClick() {
    this.dialogRef.close('no');
    }
    
    onYesClick() {
    this.dialogRef.close('yes');
    }
    
    ngOnInit() {
    }
    
    }
    

    您可以根据您的要求返回数据并将其发送给其他人。

    【讨论】:

    • 你不能通过 data.participant.participantId 获取 id 吗?
    • 我在 ngOninit(){ console.log(this.data.participant.participantId);} 中尝试
    • EcheqSelectorComponent_Host.ngfactory.js? [sm]:1 错误类型错误:无法读取 EcheqSelectorComponent.push../src/app/participant/modules/echeq/components/echeq-selector/echeq-selector.component.ts.EcheqSelectorComponent.ngOnInit 处未定义的属性“participantId” (echeq-selector.component.ts:65)
    • 你能在 stackblitz 上托管一个演示代码吗?会有帮助的
    • 我不知道怎么做,因为还有服务。
    猜你喜欢
    • 2017-09-13
    • 2019-06-12
    • 2021-11-02
    • 2018-06-05
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2017-11-04
    • 2019-01-19
    相关资源
    最近更新 更多