gpt4 book ai didi

Angular mat-dialog 未定义值 afterClosed()

转载 作者:搜寻专家 更新时间:2023-10-30 21:22:07 24 4
gpt4 key购买 nike

我一直在一个有 Angular 5 和 Angular Material 的项目中工作,我试图将值传递给对话框并在对话框关闭时取回值,但由于某种原因,当对话框关闭时我变得不确定关闭。

对话框

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);
});
}

最佳答案

为了从您的模态获取数据到您的 afterClosed() Observable,您需要将一个参数传递给您的 dialogRef.close() 方法,如下所示:

this.dialogRef.close(this.data);

关于Angular mat-dialog 未定义值 afterClosed(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50745312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com