我一直在使用角度5和角度材料的项目中工作,我尝试将值传递给对话框并在对话框关闭时获取值,但出于某种原因,当对话框出现时我得到了未定义闭合。
对话
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();
}
}
评论组件
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);
});
}
答案 0 :(得分:3)
为了将模态中的数据提供给afterClosed()
Observable,您需要将参数传递给dialogRef.close()
方法,如下所示:
this.dialogRef.close(this.data);