代码之家  ›  专栏  ›  技术社区  ›  bellotas

*ngfor子阵对象-角度+火力基地

  •  0
  • bellotas  · 技术社区  · 6 年前

    我想用一个简单的 ngFor 但我在处理一个问题。

    对象

    export class Post {
      $key: string;
      title: string;
      subtitle: string;
      postedBy: string;
      imageUrl: string;
      paragraphs: [] = [];
    }
    

    我的意图是使用以下 HTML 代码:

    <p *ngFor="let p of databaseService.selectedPost[0].paragraphs">a</p>
    

    意思是我想在我的对象的每一段中显示一个段落,但是我得到了这个错误:

    错误错误:找不到不同的支持对象“[对象对象]” 类型为“object”。ngfor只支持绑定到iterables,如 数组。

    我试着用 console.log(this.selectedPost[0].paragraphs.p1); 存在 p1 这是我的第一段,它确实有效,但我不能从 HTML (它有p1,p2,p3..作为名字)。下一张图片显示 console.log

    enter image description here

    这是数据库中的对象:

    enter image description here

    我该怎么做?

    0 回复  |  直到 6 年前
        1
  •  1
  •   ambs    6 年前

    根据事物的声音, databaseService.selectedPost[0].paragraph 是一个对象,正如你所说的,你可以直接访问 p1 .

    我试过使用console.log(this.selectedpost[0].parages.p1);p1是我的第一个段落,它实际上可以工作

    如果是这样的话,你可以使用角的 KeyValuePipe *ngFor

    一个有效的例子如下:

    <p *ngFor="let paragraph of databaseService.selectedPost[0].paragraphs | keyvalue">{{ paragraph.value }}</p>