从 angular material-dialog 中获取数据

弗拉基米尔

我需要你的帮助!我需要从“结果”中获取一个值到我的外部服务。但是它有一些麻烦,可能是因为异步工作。如何从我的组件中获取它?我的 Angular 版本是 12。这是我的代码:

import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { ModalComponent } from '../modal/modal.component';

@Injectable({
  providedIn: 'root'
})
export class ModalResultService {

  param!:string 

  constructor(
    public dialog: MatDialog
  ) { }

  dialogResult(){
    let dialogRef = this.dialog.open(ModalComponent)
    
    dialogRef.afterClosed().subscribe(
      result => {
        console.log(result);
      }
    )
  }
}

在我的外部服务中,我想做这样的事情

update(note: Notation): Observable<Notation> {
    if (this.res.dialogResult())
      return this.http.patch<Notation(`${environment.fbDbUrl}/posts/${note.id}.json`, note);
    return of(this.dNotation)
  }

dNotation 是一些默认对象

盖尔J

您需要一直返回一个Observable并使用异步代码。

dialogResult(): Observable<Boolean> {
    let dialogRef = this.dialog.open(ModalComponent)    
    return dialogRef.afterClosed();
}

在您的其他服务中:

update(note: Notation): Observable<Notation> {
    this.res.dialogResult().pipe(
      mergeMap(result => {
        if(result) { 
          return this.http.patch<Notation(`${environment.fbDbUrl}/posts/${note.id}.json`, note);
        } else {
          return of(this.dNotation);
        }
      }
    );
  }

注意:阅读 RxJs 文档mergeMap以查看它是否适合您的用例。也许另一个运营商更合适。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在“@angular/material”中找不到“MAT_DIALOG_DATA”

为什么 Angular Material Dialog 组件没有显示在 Google Chrome 中?

为什么带有 Angular Material Dialog 的 Angular 5 很慢?

如何使用ref获取Material UI中Dialog的高度?

Angular Material-Mat-Dialog-更改背景模糊/变暗效果

Angular Material2 Dialog 的父组件是什么

从Angular Material 2中的MdDialog返回数据

从Angular4上的md-dialog获取数据

计算可从后端获取的“ Angular Material Table”列中的数据总和

Angular Material-获取数据表中行的索引

Angular Material 中的标签按钮

如何将从 http 获取的数据从 Mat-Dialog 组件传递到 Angular 6 中的父组件?

Angular Material Dialogue问题-错误:选择器“ dialog-result-example-dialog”与任何元素都不匹配

使用 Material Dialog 进行警报的组件上的 Angular 单元测试未初始化

如何在Angular Material中获取活动选项卡

Angular Material Table 嵌套数据源中的 ngFor

来自其余API的数据未显示在Angular Material表中

无法在Angular JS Material Desgin中绑定数据

数据表中的Angular Material 2 routerlink

替换 @angular:material:table 中的数据源

从Angular Material Design Stepper获取步骤索引

Angular Material VirtualScrollViewPort-异步加载数据

如何从Angular Material更新垫表数据

Angular Material 6-维修台数据

Angular Material 8 中的排序表

CSS中的Angular Material 2目标元素

在Angular Material表中更改行颜色

在 Stackblitz 中单独使用 Angular Material Button?

Angular Material 2中的md-table