Angular - 带索引的 *ngFor 循环

Tru*_*Bún 6 html angular angular6

我对 Angular 非常陌生,目前我正在学习 Angular 6,它是 Angular 的最新版本。

在这里,我尝试使用从 JSON 检索的文章来设计我的博客页面,并将它们显示在两列中,如下图所示:

在此输入图像描述

标题旁边的数字是文章数组中文章的索引。很容易看出问题:从 1 开始的索引被重复两次。

这是我的代码,请告诉我为什么我的细节错误以及如何修复它。

博客服务:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class BlogService {

  constructor(private http: HttpClient) { }

  getArticleList() {
    return this.http.get('https://jsonplaceholder.typicode.com/posts');
  }
}
Run Code Online (Sandbox Code Playgroud)

文章列表组件:

从 '@angular/core' 导入 { Component, OnInit }; 从'../blog.service'导入{BlogService};

@Component({
  selector: 'app-blog',
  templateUrl: './article-list.component.html',
  styleUrls: ['./article-list.component.css']
})
export class ArticleListComponent implements OnInit {
  articles: Object;

  constructor(private data: BlogService) { }

  ngOnInit() {
    this.data.getArticleList().subscribe(
      data => this.articles = data
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

HTML 文件:

<div class="article-list-page">
    <div class="container">
      <h3>This is blog page</h3>
      <p>The industry's top wizards, doctors, and other experts offer their best advice, research, how-tos, 
          and insights, all in the name of helping you level-up your SEO and online marketing skills. Looking for the YouMoz Blog? </p>
        <span *ngFor="let index of articles; index as i">
            <div style="display: table; border: 1px solid black">
                <div class="posts-left col-md-6" style="display: table-row;">
                    <a>{{ articles[i].title }} -- {{i}}</a>
                    <p>{{ articles[i].body }}</p>
                </div>
                <div class="posts-right col-md-6" style="display: table-row;">
                    <a>{{ articles[i + 1].title }} -- {{i + 1}}</a>
                    <p>{{ articles[i + 1].body }}</p>
                </div>
            </div>
        </span>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

cod*_*ash 12

与其使用表格并尝试以这种方式应用索引,不如使用 CSS。例如:

在我的 component.ts 中:

numArray=[100,200,300,400,500,600,700,800];
Run Code Online (Sandbox Code Playgroud)

在我看来:

  <ul>
    <li *ngFor="let n of numArray; index as i">{{n}}, Index is {{i}}</li>
  </ul>
Run Code Online (Sandbox Code Playgroud)

在CSS中:

ul{
  width:400px;
}
li{
  float: left;
  list-style: none;
  width:150px;
  margin: 0px;
}
li:nth-child(even){
  margin-right: 0px;
}
Run Code Online (Sandbox Code Playgroud)

这将给出输出:

在此输入图像描述

您可以修改我给出的示例以满足您的需要。


vad*_*lim 1

TL;博士; 只需替换 HTML 文件中的此块即可

从:

<div style="display: table; border: 1px solid black">
    <div class="posts-left col-md-6" style="display: table-row;">
        <a>{{ articles[i].title }} -- {{i}}</a>
        <p>{{ articles[i].body }}</p>
    </div>
    <div class="posts-right col-md-6" style="display: table-row;">
        <a>{{ articles[i + 1].title }} -- {{i + 1}}</a>
        <p>{{ articles[i + 1].body }}</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

至:(删除 4 行)

<div style="display: table; border: 1px solid black">
    <div class="posts-left col-md-6" style="display: table-row;">
        <a>{{ articles[i].title }} -- {{i}}</a>
        <p>{{ articles[i].body }}</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

从您的屏幕截图来看,似乎只有0th不重复两次的元素。根据您的 HTML,这是预期的,因为您将其渲染两次。

您当前的模板

<span *ngFor="let index of articles; index as i">
    <div style="display: table; border: 1px solid black">
      <div class="posts-left col-md-6" style="display: table-row;">

          <!-- 1. Render the CURRENT 'article' -->
          <a>{{ articles[i].title }} -- {{i}}</a>
          <p>{{ articles[i].body }}</p>

      </div>
      <div class="posts-right col-md-6" style="display: table-row;">

          <!-- 2. Render the NEXT 'article' -->
          <a>{{ articles[i + 1].title }} -- {{i + 1}}</a>
          <p>{{ articles[i + 1].body }}</p>

      </div>
    </div>
</span>
Run Code Online (Sandbox Code Playgroud)

以下是 Angular 如何渲染articles.

  1. 渲染第 0 个元素(您的模板渲染第 0 篇和第 1 篇文章)
  2. 渲染第一个元素(您的模板渲染第一篇和第二篇文章)
  3. 渲染第二个元素(您的模板渲染第二篇和第三篇文章)
  4. 等等..

如果您只想每个元素渲染一次,只需这样做,

<div class="posts-left col-md-6" style="display: table-row;">
    <!-- 1. Render ONLY THE CURRENT 'article' -->
    <a>{{ articles[i].title }} -- {{i}}</a>
    <p>{{ articles[i].body }}</p>
</div>
Run Code Online (Sandbox Code Playgroud)

注意:已经有一篇文章充分利用了ngFor HERE

  • @TrungBún 它**确实**记得你的最后一个索引。但你渲染了两次! (2认同)