【问题标题】:Angular access ngFor index on another ngFor inside arrayAngular 访问 ngFor 索引上另一个 ngFor 内部数组
【发布时间】:2018-09-20 02:35:40
【问题描述】:

我有以下对象:

itemList = {
    "0": [],
    "1": [],
    "2": [],
    "3": []
};

我还有以下*ngFor

<div *ngFor="let new of news; let newindex = index;">
  <div *ngFor="let item of itemList.newindex">
  </div>
</div>

如您所见,我正在尝试访问itemList[0],例如使用创建的newindex

这可能吗?

【问题讨论】:

标签: javascript html angular ngfor


【解决方案1】:

尝试使用[] 符号来访问属性

<div *ngFor="let new of news; let newindex = index;">
  <div *ngFor="let item of itemList[newindex]">
  </div>
</div>

您的对象更像是数组,因此请考虑将其设为数组,而不是具有数字属性名称的对象。

查看Stackblitz

【讨论】:

  • 不工作,请注意我有一个对象(itemList),我正在打印该对象,它在数组上有值...
  • 你检查过这个例子吗?
  • 是的,并且像这样复制。请注意,我正在动态更新 itemList 值,是否与问题有关?
  • 所以这基本上是我的一个错误,我在“索引”上缺少一个“x”......但我错过了“[]”所以谢谢你。 :)
【解决方案2】:

您需要使用方括号来获取父索引newindexitemList 的值

<div *ngFor="let new of news; let newindex = index;">
  <div *ngFor="let item of itemList[newindex]">
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    相关资源
    最近更新 更多