代码之家  ›  专栏  ›  技术社区  ›  kelvin wong

Ionic 3 angularfire2打印出[对象对象]数据

  •  0
  • kelvin wong  · 技术社区  · 7 年前

    我有一个任务管理应用程序,我不想使用它 能够在每个任务部分看到他们的任务 如图所示: Click here to see the photo

    我可以从数据库中获取数据,并以JSON格式打印出来,正如您在我标记为 “从数据库获取任务” ,但问题是我不能像 “仅演示” 你可以在照片中看到的地方。

    我正在使用 角火2 从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>
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   kelvin wong    7 年前

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

    以下是管道的代码:

    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,它就会像这样了。