当前位置: 首页 > 知识库问答 >
问题:

无法显示Angular 2模板中对象的对象数组

苗信鸥
2023-03-14

我试图在angular 2模板中显示一个对象值,特别是当我试图使用ngFor获得一个对象数组时,会给我一个错误

@Component({
  selector: 'app-sg-detail',
  templateUrl: './sg-detail.component.html',
  styleUrls: ['./sg-detail.component.scss', '../../app.component.scss']
})
export class SgDetailComponent implements OnInit {

  errorMessage: string;
  sgDetail: Sg;
  groupId: string;
  ipPermissions: IpPermissions[];
  userIdGroupPairs: UserIdGroupPairs[];
  opPermissionsEgress: IpPermissionsEgress[];
  tags: Tags[];

  constructor(private activatedRoute: ActivatedRoute, private sgService: SgService) { }

  ngOnInit() {
    this.activatedRoute.params.subscribe((params: Params) => {
      let groupId = params['groupId'];
      this.groupId = groupId;
    });
    this.getSgsByGroupId(this.groupId);
  }
  getSgsByGroupId(groupId: String) {
    this.sgService.getSgByGroupId(groupId)
    .subscribe(
      sgDetail => this.sgDetail = sgDetail,
      error => {
        this.errorMessage = error.getMessage();
        console.error(error.getDescription());
      });
  }
}
 <div class="container app_container">
    <ul class="app_list">
        <li *ngIf="sgDetail" class="app_list-item">
            <p>GroupId: {{sgDetail.groupId}}</p>
            <p>GroupName: {{sgDetail.groupName}}</p>
        </li>
        <li *ngFor="let tag of sgDetail.tags" class="app_list-item">
             //this loop sgDetail.tags errors out 
        </li>
    </ul>
 </div>

这是我的模型

export interface Sg {
 ownerId: number;
 groupName: string;
 groupId: string;
 description: string;
 ipPermissions: IpPermissions[];
 ipPermissionsEgress: IpPermissionsEgress[];
 vpcId: string;
 tags: Tags[];
}

export interface IpPermissions {
 ipProtocol: string;
 fromPort: number;
 toPort: number;
 userIdGroupPairs: UserIdGroupPairs[];
 ipRanges: Array<number>;
 prefixListIds: Array<number>;
}

export interface UserIdGroupPairs {
 userId: number;
 groupName: string;
 groupId: string;
 vpcId: string;
 vpcPeeringConnectionId: string;
 peeringStatus: string;
}

export interface IpPermissionsEgress {
 ipProtocol: string;
 fromPort: number;
 toPort: number;
 userIdGroupPairs: UserIdGroupPairs[];
 ipRanges: Array<number>;
 prefixListIds: Array<number>;
}

export interface Tags {
 key: string;
 value: string;
}

共有1个答案

毕宇
2023-03-14

该错误是由Asynchorouse调用-->getsgsbygroupid引起的,这意味着当angular呈现视图时,getsgsbygroupid的结果还没有出现。

尝试angular提供的安全方式:

<li *ngFor="let tag of sgDetail?.tags" class="app_list-item">
</li>
 类似资料:
  • 问题内容: 只是试图在视图中显示我通过ajax从控制器获取的数组,但它显示的是[object Object],[object Object]而不是array。请在下面检查我的js文件: 问题答案: 使用转换到。 试试这个: 我认为,这对您有帮助-

  • 我使用带thymeleaf 3的Spring靴 我试着展示一个豆子 在我的控制器里我有 在我的车里 在我的位置 在我的胸腺碎片中 当我试图显示这个片段时,我得到 org.attoparser。ParseException:异常评估SpringEL表达式:“cities.id”由:org.springframework.expression.spel引起。SpelEvaluationExceptio

  • id Number - 与显示相关联的唯一标识符。 rotationNumber -顺时针方向的屏幕旋转角度, 可选0,90,180,270。 scaleFactor Number - 输出设备的像素比例因子。 touchSupport String - 是否支持触摸,可选 available, unavailable, unknown. bounds Object Rectangle​ size

  • 我创建了Person类,它由类的Student和Employee扩展(由其他Employee类型的类扩展)。person类如下所示: 我目前正在尝试创建一个方法,它将显示所有的人,如果他们是gender=“male”。我有: 我不确定如何引用方法中的对象(学生和雇员,它们都是persons)来返回它们。而我也不知道在主法中如何引用这个方法。我不能用个人打印,但如果我用 然后使用 我只是返回jame

  • 在JS中,如果我将一个字符串记录到控制台,它是否没有正确显示? 后果 那么为什么显示的是[object],而不是值呢?它就像是在附加字符串的情况下更改类型?

  • 我将spring boot与thymeleaf结合使用,在页面中尝试显示表单及其嵌套对象。 我的对象工厂有 id 当我显示时 我的工厂有很多机器,但没有一台展出 任何想法?