【问题标题】:Ionic 3 angularfire2 print out [object Object] dataIonic 3 angularfire2 打印出 [object Object] 数据
【发布时间】:2018-04-30 02:48:42
【问题描述】:

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

我可以从数据库中获取数据并以 JSON 格式打印出来,如您在照片中将其标记为 “从数据库中获取任务”,但问题是我不能像 “仅限演示” 一样显示它,您可以在照片中看到。

我正在使用 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>

【问题讨论】:

    标签: ionic-framework ionic3 angularfire2 angularfire5


    【解决方案1】:

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

    这是管道的代码:

    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 即可。

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 2018-09-25
      • 2018-08-26
      • 1970-01-01
      • 2018-03-26
      • 2018-02-26
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      相关资源
      最近更新 更多