Ionic 3 angularfire2 打印出 [object Object] 数据

Ionic 3 angularfire2 print out [object Object] data

我有一个任务管理应用程序,我希望用户能够在每个任务部分看到他们的任务,就像照片中一样:Click here to see the photo

我可以从数据库中获取数据并以 JSON 格式打印出来,正如您在照片中看到的那样,我将其标记为 "task get from database",但问题是我无法像您在照片中看到的 "Demo only" 那样显示它。

我正在使用 angularfire2 从 firebase 实时数据库获取数据。

这里是数据的控制台:The console of the data

这是来自提供商的代码:

getTaskSectionAndTaskList(projBoardKey) {
    this.taskSectNTaskListRef = this.afDatabase.list('taskSection/' + projBoardKey);

    return this.taskSectNTaskListRef;
}

以下是 .ts 文件中的代码:

...
taskList$: Observable<Task[]>;

constructor(..., private tasksecProvider: TasksectionProvider) {
    ...

    this.taskList$ = this.tasksecProvider.getTaskSectionAndTaskList(this.selectedBoardKey)
    .snapshotChanges()
    .map(changes => {
        return changes.map(c => ({
            key: c.payload.key,
            ...c.payload.val()
        }));
    });
}

这里是 HTML 文件的代码:

<div *ngFor="let taskSectionList of taskList$ | async">
    <ion-slide>
        <ion-card>
            <ion-card-header>
                <p class="task-section-title">{{ taskSectionList.taskSectionTitle }}</p>
            </ion-card-header>
            <ion-card-content>
                <div>
                    <div class="for-demo-only">
                        <div>
                            <p>Task Name</p>
                        </div>
                    </div>
                    <div>
                        {{ taskSectionList.task | json }}
                    </div>
                </div>
            </ion-card-content>
        <ion-card>
    </ion-slide>
</div>

我已经使用管道解决了我的问题:

管道代码如下:

import { Pipe, PipeTransform } from '@angular/core';

/**
 * Generated class for the GettaskPipe pipe.
 *
 * See https://angular.io/api/core/Pipe for more info on Angular Pipes.
 */
@Pipe({
   name: 'gettask',
})
export class GettaskPipe implements PipeTransform {
  /**
   * Takes a value and makes it lowercase.
   */
  transform(obj: any) {

    let tempResult = [];
    let result = [];

    for (var key in obj.task) {
      if (obj.task.hasOwnProperty(key)) {
        result.push({
            key: key,
            ...obj.task[key]
        });
      }
    }

    return result;
  }
}

这是我对 HTML 文件所做的更改:

<div *ngFor="let taskSectionList of taskList$ | async">
<ion-slide>
    <ion-card>
        <ion-card-header>
            <p class="task-section-title">{{ taskSectionList.taskSectionTitle }}</p>
        </ion-card-header>
        <ion-card-content>
            <div>
                <div class="for-demo-only">
                    <div>
                        <p>Task Name</p>
                    </div>
                </div>
                <div *ngFor="let task of taskSectionList | gettask">
                    {{ task.taskName }}
                </div>
            </div>
        </ion-card-content>
    <ion-card>
</ion-slide>

现在我可以让任务名称像演示一样显示了,只需要对其应用一些 css 然后它就会看起来像那样了。