简体   繁体   English

* ngFor指令不适用于数组数组

[英]*ngFor directive not working on array of arrays

I have an array of arrays that I want to use as a directory. 我有一个要用作目录的数组数组。 The point is to sort all the items according to which letter they begin with. 关键是要根据它们以哪个字母开头对所有项目进行排序。

Here is the code of my item list component : 这是我的商品清单组件的代码:

let itemList = ["Plane","Bird","Boat"];
let directory = [];
for (const item of this.itemList) {
    if (this.directory[item.charAt(0).toLowerCase()] == null || undefined) {
        this.directory[item.charAt(0).toLowerCase()] = [];
    }
    this.directory[item.charAt(0).toLowerCase()].push(item);
}

Then I want to display all my items sorted according to their first letter, and I want that first letter to be displayed above the list of items that begin with it, just like a directory. 然后,我想显示按照首字母排序的所有项目,并且希望将首字母显示在以它开头的项目列表上方,就像目录一样。 I use the following template in HTML : 我在HTML中使用以下模板:

<div id="item-list">
  <div *ngFor="let entry of directory ; let i = index">
    <p>{{i}}</p>
    <div *ngFor="let item of entry">
      <p>{{item}}</p>
      <app-item></app-item>
    </div>
  </div>
</div>

However, when I run my app, I see no data displayed in HTML. 但是,当我运行我的应用程序时,没有看到HTML显示的数据。 I tried to print text at several points : 我尝试在几个点上打印文本:

<div id="item-list">
  <p>Hello</p>
  <div *ngFor="let entry of directory ; let i = index">
    <p>Hello again</p>
    <p>{{i}}</p>
    <div *ngFor="let item of entry">
      <p>{{item}}</p>
      <app-item></app-item>
    </div>
  </div>
</div>

The webpage displays"Hello", but no "Hello again" (while I think the latter should be printed twice). 该网页显示“ Hello”,但不显示“ Hello again”(我认为后者应打印两次)。 However, there is absolutely no error message neither when running ng serve nor loading the page. 但是,在运行ng serve或加载页面时,绝对没有错误消息。 I've searched on the web for a similar problem, but nobody seems to be facing the same difficulties to dynamically display an array of arrays. 我在网上搜索了类似的问题,但是似乎没有人会面临同样的困难来动态显示数组。

Can you tell me what I am doing wrong? 你能告诉我我做错了什么吗? Any help would be greatly appreciated. 任何帮助将不胜感激。 :) :)

stackblitz: https://stackblitz.com/edit/angular-tujusv stackblitz: https ://stackblitz.com/edit/angular-tujusv

The directory array is a key:value pair array. 目录数组是一个key:value对数组。 You can store the keys in a different array and access them in the html 您可以将密钥存储在其他数组中,并在html中访问它们

export class ItemListComponent implements OnInit {
  alphabet: string[];
  directory = [];
  dirArr =[];
  itemList = ["Plane", "Bird", "Boat"];
  constructor() {
    this.alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

  }


  ngOnInit() {

    this.itemList.sort();
    for (const item of this.itemList) {
      if (this.directory[item.charAt(0).toLowerCase()] == null || undefined) {
        this.directory[item.charAt(0).toLowerCase()] = [];
      }
      this.directory[item.charAt(0).toLowerCase()].push(item);
      console.log(this.directory);
      this.dirArr = Object.keys(this.directory);
      console.log(this.dirArr)

    }

    console.log(this.directory['p']);


  }
}

HTML: HTML:

<div id="item-list">
  <p>Hello</p>
  <div *ngFor="let entry of dirArr ; let i = index">
    <p>Hello again</p>
    <p>{{i}}</p>
    <p>{{entry}}</p>
    <p>{{directory[entry]}}</p>
    <div *ngFor="let item of directory[entry]">
      <p>{{item}}</p>
    </div>
  </div>
</div>

make one temp variable and your other code is fine: only change below code: 设置一个临时变量,其他代码也可以:仅更改以下代码:

let itemList = ["Plane","Bird","Boat"];
  let temp = {};
  let directory = []

 for (const item of this.itemList) {

    if (this.temp[item.charAt(0).toLowerCase()] == null || undefined) {
        this.temp[item.charAt(0).toLowerCase()] = [];
    }
    this.temp[item.charAt(0).toLowerCase()].push(item);

  }
   this.directory = Object.values(this.temp)

Keyed and nested structures in JavaScript are constructed using OLN. JavaScript中的键控和嵌套结构是使用OLN构造的。 They support for key:value pairs in the way you are attempting to structure your data. 它们以您试图构造数据的方式支持key:value对。

Your use case data structure seems to be: 您的用例数据结构似乎是:

{
  b : ["Bird", "Boat"],
  p  : ["Plane"]
}

But you are attempting to construct: 但是您正在尝试构造:

[
  b => [ "Bird", "Boat"], p => ["Plane"]
]

Which is not valid in JavaScript. 在JavaScript中无效。

Change your declaration of directory from [] to {} directory声明从[] 更改{}

I've done the below changes to accomplish your requirement try it once. 我已经做了以下更改,以满足您的要求,请尝试一次。

this.items = this.items.sort();
let currentLetter = this.items[0].charAt(0);
let currentLetterLib = [];
for (let item of this.items) {
  if (item.charAt(0).toLowerCase() != currentLetter) {
      this.directory.push({
        "letter": currentLetter,
        "words": currentLetterLib
      });
      currentLetter = item.charAt(0).toLowerCase();
      currentLetterLib = [];
  }
  currentLetterLib.push(item);
}
this.directory.push({
  "letter": currentLetter,
  "words": currentLetterLib
});

<div id="item-list">
  <p>Hello</p>
  <div *ngFor="let entry of directory ;">
    <p>{{entry.letter}}</p>
    <div *ngFor="let item of entry.words">
      <p>{{item}}</p>
    </div>
  </div>
</div>

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

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