首页 文章

角度mat-dialog未定义值afterClosed()

提问于
浏览
0

我一直在使用角度5和角度材料的项目中工作,我尝试将值传递给对话框并在对话框关闭时获取值,但由于某种原因,当对话框关闭时我得到了未定义 .

Dialog

import { Component, OnInit, Inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ReaderService } from '../../../../../services/reader/reader.service';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
@Component({
  selector: 'fuse-comment-dialog',
  templateUrl: './comment-dialog.component.html',
  styleUrls: ['./comment-dialog.component.scss']
})
export class CommentDialogComponent implements OnInit {

  public commentsForm: FormGroup;
  constructor(
    private fb: FormBuilder,
    private readerService: ReaderService,
    public dialogRef: MatDialogRef<CommentDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) { }

  ngOnInit() {
    this.commentsForm = this.fb.group({
      commentText: ['', [
        Validators.required
      ]],
      commentType: 'PrivateComment',
      commentGroup: 'Therabytes'
    });
  }

  public sendComent(): void {
    this.data['text'] = this.commentsForm.value.commentText;
    this.data['commentVisibility'] = this.commentsForm.value.commentType;
    this.readerService.newComment(this.data)
      .then((commentId) => {
        this.data['id'] = commentId;
        this.dialogRef.close();
      });
  }

  public closeDialog(): void {
    this.commentsForm.value.commentText = '';
    this.dialogRef.close();
  }

}

Comment Component

public commentDialog(): void {
    let newComment = {
      documentId: this.document.id
    };
    let commentDialogRef = this.dialog.open(CommentDialogComponent, {
      width: '300px',
      data: newComment
    });
    commentDialogRef.afterClosed().subscribe(comment => {
      console.log(comment);
      this.comments.push(comment);
    });
  }

1 回答

  • 1

    为了将模态中的数据提供给 afterClosed() Observable,您需要将参数传递给 dialogRef.close() 方法,如下所示:

    this.dialogRef.close(this.data);
    

相关问题