【问题标题】:Adding property in Observable data before returning through connect function of angular material在通过角度材料的连接函数返回之前在 Observable 数据中添加属性
【发布时间】:2018-11-23 14:01:03
【问题描述】:

我正在使用角度材料表。在连接函数内部,我正在返回一个可观察的患者类型。但是我必须在返回之前更新 PatientData observable 的值。如果患者 ID 存在于 redrows 数组中,我必须添加一个“Active 属性。

数据表的连接函数

connect(): Observable<Patient[]> {
    const patientData = this.patientService.getPatient();

    patientData.subscribe((jsonData) => {
      const redRows = this.getAge(jsonData);
      for (let a = 0; a < jsonData.length; a++) {
        if (redRows.includes(jsonData[a].id)) {
          jsonData[a].active = 'Active';
        } else {
          jsonData[a].active = 'Not Active';
        }
      }
      return jsonData;

    },
      (err) => console.error(err),

      () => console.log('observable complete'));
    return patientData;
 }

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    修改数据并从subscription 返回不会改变可观察对象,不要在connect() 中更改subscribe()。使用rxjs 中的map 运算符来修改数据。

    import { map} from "rxjs/operators"
    
    connect(): Observable<Patient[]> {
        return this.patientService.getPatient().pipe(
            map((jsonData) => {
                const redRows = this.getAge(jsonData);
                for (let a = 0; a < jsonData.length; a++) {
                    if (redRows.includes(jsonData[a].id)) {
                        jsonData[a].active = 'Active';
                    } else {
                        jsonData[a].active = 'Not Active';
                    }
                }
                return jsonData;
            })
        )
    }
    

    subscribeconnect() 无论您在哪里使用它。

    【讨论】:

    • @SoumyaBajpai:如果它有效,你愿意接受答案吗:D
    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多