简体   繁体   English

ngFor内部ngFor导致错误

[英]ngFor inside ngFor causing error

I have a firebase json object which consists of sections. 我有一个由部分组成的firebase json对象。 Inside the sections is also section threads. 在节内部还有节线程。 I'm trying to iterate the sections, and then the threads. 我正在尝试迭代各个部分,然后迭代线程。 Problem is I'm getting this error 问题是我遇到此错误

InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' InvalidPipeArgument:管道“ AsyncPipe”的“ [对象对象]”

when I add the second *ngFor for the threads 当我为线程添加第二个* ngFor时

This is my HTML 这是我的HTML

<div *ngFor="let section of forumSections | async">
  <div>Header title: {{section.sectionTitle}}</div>

<div *ngFor="let thread of section.sectionThreads | async">
  <div>Thread title: {{thread.title}}</div>
</div>

And my JSON object from my firebase database 还有我的Firebase数据库中的JSON对象

 "forum" : {
"sections" : {
  "Y6ML8AA9V5RB2sKFmKndnHFqRw23" : {
    "sectionThreads" : {
      "-zqehRPSbalaburpm2dW" : {
        "description" : "Sup ladies",
        "title" : "elo mm9"
      },
      "-zqehRPSbalajYGfm2dW" : {
        "description" : "Sup boi",
        "title" : "elo m8"
      }
    },
    "sectionTitle" : "Official"
  }
}

} }

Am I missing something? 我想念什么吗? Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

Update: 更新:

forum.ts 论坛

    import {Component, OnInit} from '@angular/core';
import {IonicPage} from 'ionic-angular';
import {FirebaseListObservable} from "angularfire2";
import {ForumServiceProvider} from "../../providers/forum-service/forum-service";

@IonicPage()
@Component({
  selector: 'page-forum',
  templateUrl: 'forum.html',
})

export class ForumPage implements OnInit {

  forumSections: FirebaseListObservable<any>;

  constructor(public forumService: ForumServiceProvider) {
  }

  ngOnInit() {
    this.loadForumData();
  }

  loadForumData() {
    this.forumSections = this.forumService.getInitialSections();
  }

}

Forum Service 论坛服务

import {Injectable} from '@angular/core';
import 'rxjs/add/operator/map';
import {AngularFire, FirebaseListObservable} from "angularfire2";

@Injectable()
export class ForumServiceProvider {

  constructor(public af: AngularFire) {
  }

  /**
   * Get the forums front facing sections
   * @returns {FirebaseListObservable<any>}
   */
  getInitialSections(): FirebaseListObservable<any> {
    return this.af.database.list('/forum/sections');
  }

}

Async pipe needs an Observable, make sure your response is Observable, since you are using firebase, it should be FirebaseListObservable. 异步管道需要一个Observable,请确保您的响应是Observable,因为您正在使用firebase,因此它应该是FirebaseListObservable。

in your case, I think if you remove the | async 以您的情况,我认为如果您删除| async | async in your second ngFor it will fix your issue since it's an array. 在第二个ngFor中| async ,因为它是一个数组,它将解决您的问题。

from your comment, it looks like your first ngfor should work, second one wont work because it's not an observable anymore, nor an array . 从您的评论看来,您的第一个ngfor应该工作,第二个不能工作,因为它不再是可观察的了,也不是数组。 so you need to convert second one into an array, also remove the asyc pipe in second one . 因此,您需要将第二个转换为数组, 还需要删除第二个的asyc管道

 this.af.database.list('/forum/sections/') .map(sections=>sections .map(section=>Object.keys(section.sectionThreads) .map(key=>section.sectionThreads[key]))) 

I haven't tested it, and it's not typescript, you might need to do some conversion 我没有测试过,它不是打字稿,您可能需要进行一些转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM