简体   繁体   English

如何在Angular2中显示该JSON

[英]How do I display that JSON in Angular2

How do I display this specific JSON using *ngFor? 如何使用* ngFor显示此特定的JSON?

{
    "status": 0,
    "dallases": [{
        "vehicle_id": 17954,
        "dallassettings": "3",
        "dallasupdated": "False",
        "dallas_list": [{
            "number": 666111222,
            "auth": 3
        }, {
            "number": 666777888,
            "auth": 4
        }, {
            "number": 123454321,
            "auth": 4
        }]
    }
}

vehicle.service.ts 车辆服务

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Jsonp, Response } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class VehicleService {
    constructor(private http: Http) { }

    getVehicle() {
        return this.http.get('myURL')
            .map(res => res.json());
    }
}

vehicle.component.ts 车辆组件

import { Component, enableProdMode } from '@angular/core';
import { VehicleService } from './vehicle.service';

enableProdMode();

@Component({
  //moduleId: module.id,  
  selector: 'vehicle-json',
  templateUrl: './vehicle.html',
  providers: [VehicleService]
})
export class VehicleComponent {
  //vehicles: Vehicle[];
  vehicles: GeneralVehicle[];

  constructor(private vehicleService: VehicleService) {
    this.vehicleService.getVehicle().subscribe(vehicle => {
      this.vehicles = vehicle;
    });
  }
}

/*interface Vehicle {
  id: number;
  title: string;
  body: string;
}*/
interface GeneralVehicle {
  status: number;
  dallases: Vehicle[];
}

interface Vehicle {
  vehicle_id: number;
  dallassettings: number;
  dallasupdated: string;
  dallas_list: DallasList[];
}

interface DallasList {
  number: number;
  auth: number;
}

When I worked on dummy data it was simply, but this JSON structure is more complex. 当我处理伪数据时,它很简单,但是此JSON结构更复杂。 I tried using Pipe, but I'm not sure if I'm using it correctly. 我尝试使用Pipe,但是不确定是否正确使用它。

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'keys' })
export class VehiclePipe implements PipeTransform {
    transform(value, args: string[]): any {
        let keys = [];
        for (let key in value) {
            keys.push(key);
        }
        return keys;
    }
}

That's the *ngFor 那就是* ngFor

*ngFor="let vehicle of vehicles | keys"

I'd like to dislay status once, and then repeat all the dallases (Vehicle[]). 我想取消一次状态,然后重复所有达拉斯(Vehicle [])。

You have to loop through 2 ngFor's: 您必须遍历2个ngFor:

<div *ngFor="let vehicle of vehicles | keys">
    <h1>{{ vehicle.status }}</h1>
    <ul *ngFor="let dallas of vehicle.dallases">
        <li>{{ dallas | json }}</li> //Show the data you want
    </ul>
</div>

vehicles is an object. 车辆是一个对象。 I see no need to iterate over the keys with the pipe you proposed. 我认为没有必要使用您建议的管道来遍历密钥。 You can access the members you need directly. 您可以直接访问所需的成员。 You should iterate over the dalasses property of vehicles - which is an array. 您应该遍历车辆的dalasses属性-这是一个数组。 Then display the array member as you require. 然后根据需要显示数组成员。 Eg with a json pipe to get in text format, you you might also implement custom formatting inside the template through properties. 例如,使用json管道获取文本格式,您还可以通过属性在模板内部实现自定义格式。

Example: 例:

<div>
  <h1>{{ vehicle.status }}</h1>
  <div *ngFor="let vehicle of vehicles.dallases">
    <div>ID: {{vehicle.vehicle_id}}</div>
    <div>Settings: {{vehicle.dallassettings}}</div>
    <div>Updated: {{vehicle.dallasupdated}}</div>
    <div *ngFor="let d of vehicle.dallas_list">
      <div>number: {{d.number}}</div>
      <div>auth: {{d.auth}}</div>
    </div>
  </div>
</div>

At least you have one error here: 至少您在这里有一个错误:

dallassettings: '3' // string

but in your interface you have: 但是在您的界面中,您有:

dallassettings: number;

Then if you implement Matthiases implementation, put a *ngIf -statement, since I suspect you get your data async, so view is rendered before data retrieved, which would cause a undefined error. 然后,如果您实现Matthiases实现,请放置*ngIf -statement,因为我怀疑您的数据是异步的,因此在检索数据之前会先呈现视图,这将导致未定义的错误。

<div *ngIf="vehicles"> // here!
  <h1>{{ vehicles.status }}</h1>
  <div *ngFor="let vehicle of vehicles.dallases">
    <div>ID: {{vehicle.vehicle_id}}</div>
    <div>Settings: {{vehicle.dallassettings}}</div>
    <div>Updated: {{vehicle.dallasupdated}}</div>
    <div *ngFor="let d of vehicle.dallas_list">
      <div>number: {{d.number}}</div>
      <div>auth: {{d.auth}}</div>
    </div>
  </div>
</div>

EDIT your status is undefined, since you you are using the wrong attribute it should be vehicles.status with "s". EDIT您的状态未定义,因为您使用的属性错误,因此应为vehicles.status并带有“ s”。 When I edited this post, I also added the "s" to the answer, so that it would be correct :) 当我编辑这篇文章时,我也在答案中加上了“ s”,这样才是正确的:)

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

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