简体   繁体   English

ngfor 指令无法正常工作

[英]The ngfor directive is not working properly

I'm very new to Angular and I can't solve this issue.我对 Angular 很陌生,我无法解决这个问题。 I'm working on a site designed by others and I have to display three cards under a banner in a page called api lab.我正在开发一个由其他人设计的网站,我必须在名为 api 实验室的页面中的横幅下显示三张卡片。 So I created the api-lab component, where I initialize a list I want to display.所以我创建了 api-lab 组件,在这里我初始化了一个我想要显示的列表。

This is the important stuff in api-lab.component.ts:这是 api-lab.component.ts 中的重要内容:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-api-lab',
  templateUrl: './api-lab.component.html',
  styleUrls: ['./api-lab.component.scss']
})
export class ApiLabComponent implements OnInit {
  apiList: Array<ApiCategory>;
  apiTitle: string;
  apiDescription: string;
  landingBackgroundImage: string;
  accountInformationImage = '/assets/images/home/box_account-information.jpg';
  accountInformationIcon = '/assets/images/home/accountInformation_u.png';

  constructor() {
    ...
    });
  }
  ngOnInit() {
    this.createApiCAtegoryList();
  }
...
  createApiCAtegoryList() {}
}
...
class ApiCategory {}

This is the api-lab.component.html:这是 api-lab.component.html:

<div class="row">
  <div class="col-24 col-md-16 offset-md-4 col-xl-16 offset-xl-4">
    <hero-top [title]=apiTitle [description]=apiDescription [backgroundImage]=landingBackgroundImage
        [textSide]="'right'" [buttonEnabled]=true [buttonText]="'Get started'" [buttonLinkToPage]="'/get-started'">
    </hero-top>
  </div>
</div>
<div class="row mt-4">
  <div class="col-24 col-md-16 offset-md-4 col-xl-16 offset-xl-4">
    <div class="row no-guttter">

HERE:
        <div class="col-md-8 " *ngFor="let category of apiList">
            <api-lab-category-card [image]="category.image" [icon]="category.icon" [title]="category.title"
              [description]="category.description" [buttonLink]="category.buttonLink"></api-lab-category-card>
    </div>
  </div>
</div>
<div class="col-24 col-md-16 offset-md-4 col-xl-16 offset-xl-4">
</div>

When i load the page without the ngFor, I can see the banner normally.当我在没有 ngFor 的情况下加载页面时,我可以正常看到横幅。 When I try to display the cards with ngFor, the entire site goes blank.当我尝试使用 ngFor 显示卡片时,整个站点变为空白。 As I told, I'm very new to Angular but I have to solve this problem to go on.正如我所说,我对 Angular 很陌生,但我必须在 go 上解决这个问题。 If you need any detail I'll gladly provide you.如果您需要任何详细信息,我很乐意为您提供。 Thanks for reading and help me.感谢您阅读并帮助我。

the thing is you have not closed on div tag inside the api-lab.component.html file.问题是您尚未关闭 api-lab.component.html 文件中的 div 标签。

Just updated your code and added an unclosed div so please my copy my code into your file and this will work:)刚刚更新了您的代码并添加了一个未关闭的 div,所以请将我的代码复制到您的文件中,这将起作用:)

Please don't forget to mark my question as a solution if it helps.如果有帮助,请不要忘记将我的问题标记为解决方案。 Thanks谢谢

  <div class="col-24 col-md-16 offset-md-4 col-xl-16 offset-xl-4">
    <hero-top [title]=apiTitle [description]=apiDescription [backgroundImage]=landingBackgroundImage
      [textSide]="'right'" [buttonEnabled]=true [buttonText]="'Get started'" [buttonLinkToPage]="'/get-started'">
    </hero-top>
  </div>
</div>
<div class="row mt-4">
  <div class="col-24 col-md-16 offset-md-4 col-xl-16 offset-xl-4">
    <div class="row no-guttter">

      HERE:
      <div class="col-md-8 " *ngFor="let category of apiList">
        <api-lab-category-card [image]="category.image" [icon]="category.icon" [title]="category.title"
          [description]="category.description" [buttonLink]="category.buttonLink"></api-lab-category-card>
      </div>
    </div>
  </div>
</div>
<div class="col-24 col-md-16 offset-md-4 col-xl-16 offset-xl-4">
</div> 

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

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