简体   繁体   English

Angular 将 object 数组传递给 html ngFor 问题

[英]Angular pass object array to html ngFor problem

I have angular on the frontend and .net-core on the backend in my project.在我的项目中,前端有 angular 和后端有 .net-core。 I simply use http get call for getting members array.我只是使用 http 获取获取成员数组的调用。 I would like the list my members name.我想要我的成员姓名列表。 my code is like that;我的代码是这样的;

<div *ngFor = "let member of members">
  <span>{{member.name}}</span>
</div>

But it doesn't work.但它不起作用。 My Http get works with status code 200 when I look at network tab on chrome's inspect screen but nothing show on the page fully blank.当我查看 chrome 的检查屏幕上的网络选项卡时,我的 Http 的状态代码为 200,但页面上没有任何显示完全空白。 I tried to add extra code for detecting my error and editted my code like this;我尝试添加额外的代码来检测我的错误并像这样编辑我的代码;

<div>
  <span>{{members[0].name}}</span>
</div>
<div *ngFor = "let member of members">
  <span>{{member.name}}</span>
</div>

It works... I can show all members name on the page but have an error on console "cannot read property of member[0] of undefined"它有效...我可以在页面上显示所有成员名称,但在控制台上出现错误“无法读取未定义成员 [0] 的属性”

Interesting problem, do you have any idea?有趣的问题,你有什么想法吗?

Btw, I tried add question mark to my array like <span>{{memeber[0]?.name}}</span> but still have same error.顺便说一句,我尝试向我的数组添加问号,例如<span>{{memeber[0]?.name}}</span>但仍然有同样的错误。 Actually I don't need this below part of the code;实际上我不需要这部分代码;

<div>
  <span>{{members[0].name}}</span>
</div>

But I couldn't get members name on the page without this code-part.但是如果没有这个代码部分,我就无法在页面上获取成员名称。

I couldn't reach my codes now sorry.抱歉,我现在无法访问我的代码。 But I try to explain what am I did on my member.ts class;但我试图解释我在 member.ts class 上做了什么;

1- I create memberModel and contains name, age, nationality, livingCountry 2- I post httpGet call to my backend and it retrive member infos from postgre db. 1-我创建 memberModel 并包含姓名、年龄、国籍、livingCountry 2-我将 httpGet 调用发布到我的后端,它从 postgre db 中检索成员信息。 3- In member.ts class 3.1- I define empty members array. 3- 在 member.ts class 3.1- 我定义空成员数组。 members: memberModel[];成员:成员模型[]; 3.2- create a method getMembers() which subscribe to my http get method. 3.2-创建一个方法 getMembers() 订阅我的 http get 方法。 3.3- I call getMembers() on ngOnIni method. 3.3- 我在 ngOnIni 方法上调用 getMembers()。

I guess your issue relies with the fetching of members.我想您的问题取决于获取成员。 You said that you are getting this data from a.Net backend.您说您从.Net 后端获取这些数据。 Maybe your request is coming back later and this is why members is undefined when the component firstly loads.也许您的请求稍后会回来,这就是组件首次加载时未定义成员的原因。 I would suggest you define the members array with a default value in the component.ts file:我建议您在 component.ts 文件中使用默认值定义成员数组:

public members = [];

Also, to further help, it would be very helpful to put more info, also the typescript file would help.此外,为了进一步提供帮助,提供更多信息会非常有帮助,typescript 文件也会有所帮助。

Here is a whole working demo you could follow:这是您可以遵循的完整工作演示:

import { Component,Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
})
export class HomeComponent {
  public members: memberModel[];

  constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    http.get<memberModel[]>(baseUrl +'weatherforecast/getmember').subscribe(result => {
      this.members = result;
    }, error => console.error(error));
  }
}
interface memberModel {
  name: string;
  age: number;
  nationality: string;
  livingCountry: string;
}

Backend Code:后端代码:

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    [HttpGet]
    [Route("getmember")]
    public IEnumerable<memberModel> getmember()
    {
        var model = new List<memberModel>()
         {
             new memberModel(){Age=12,Name="aa",Nationality="China",LivingCountry="ShangHai"},
             new memberModel(){Age=25,Name="bb",Nationality="USA",LivingCountry="Washington"},
             new memberModel(){Age=32,Name="cc",Nationality="UK",LivingCountry="London"}
         };
        return model;
    }
}

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

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