首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在物体数组中不显示物体的角度

在物体数组中不显示物体的角度
EN

Stack Overflow用户
提问于 2020-04-19 14:45:23
回答 2查看 481关注 0票数 1

我用角度从api中获取数据。我拥有的数据结构是数组(对象).array(对象)

  • 这是我的数据

  • ,这是组件ts

代码语言:javascript
复制
export class LoginComponent implements OnInit {

collaborator:Collaborator[]=[];
entreprise:Entreprise[]=[];


  constructor(private serv:EntrepriseService) { }

 getcoll(){
  this.serv.getcollaboratorses().subscribe(res =>{
     this.collaborator = res.content;
     // this.entreprise=res.content.entreprise;
      console.log(res);
    },err =>{

      console.log(err);
    });
}

协作模块

代码语言:javascript
复制
import {Entreprise} from './entreprise';
export interface Collaborator {

    id:String;
    name:String;
    email:String;
    password:String;
    phone:String;
    entreprise:Entreprise[];
    roles:any[];
}

  • 企业模块

代码语言:javascript
复制
export interface Entreprise {

    id:String;
    name:String;
    socialPurpose: String;
    businessCode:String;
    activityDomain:String;
    email:String;
    password:String;
    logo:String;
    roles:any[];

}

  • html代码

代码语言:javascript
复制
<p *ngFor="let ee of collaborator">
   {{ee.name}} - <ng-container *ngIf="ee?.entreprise"> {{ee.entreprise.name}} </ng-container>

  </p>

当我删除这部分<ng-container *ngIf="ee?.entreprise"> {{ee.entreprise.name}} </ng-container>时,代码工作正常,浏览器可以分解所有协作者的名称,但是当我试图显示他们的角色或企业时,我不能。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-19 18:00:39

ee.entreprise是一个数组,名称不存在。使用另一个ngfor来扭曲它,如下面的代码所示

代码语言:javascript
复制
<p *ngFor="let colab of collaborator">
   {{colab.name}} - 
    <ng-container *ngIf="colab.entreprise?.length"> 
        <span *ngFor="let ee of colab.entreprise"> 
            {{ee.name}} 
        </span>
    </ng-container>
  </p>
票数 2
EN

Stack Overflow用户

发布于 2020-04-19 14:52:40

ee.enterprise是一个数组,ee.enterprise.name不存在。首先,必须将其编入索引。例如,"ee.enterprise[0].nameee.enterprise[1].name等。

最有可能的是,ngFor数组将需要另一个ee.enterprise

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61306227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档