如何在angular2中使用foreach获取json

时间:2017-11-17 10:03:25

标签: json angular angular2-template

大家好我正在研究Angular应用程序,我想帮助从json获取数据。我用我的方法得到了我的conpoments.ts但它没有用。我包含了json文件。请帮我在html页面上显示数据。谢谢

label: "car"
manufacture:Array(3)
{id: "1", label: "name", question: "", type: "type", value: {…}}
{id: "2", label: "name", question: "", type: "type", value: {…}}
{id: "3", label: "name", question: "", type: "type", value: {…}}


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

import { ManufactureService } from '../../services/ManufactureService/manufacture.service';


@Component({
  selector: 'body-content',
  templateUrl: './body.component.html',
  styleUrls: ['./body.component.less']
})
export class BodyComponent implements OnInit {
  constructor(public 
  manufactureService:ManufactureService) { }
  async ngOnInit() {

  const [err,data] = await  this. manufactureService.getManufature('car','man').toPromise()

   if (err) {
        console.log(err)
        return false
    } 

    showCar(data){
    data.forEach(el=>{
      this.manufactureService.getManufature.push();
    })
  }

  }
}

1 个答案:

答案 0 :(得分:0)

Try This :

import HttpClientModule

export class MyComponent implements OnInit {

  constructor(private manufactureService: ManufactureService) {}

  ngOnInit(): void {
    this. manufactureService.getManufature(subscribe(data => {
      console.log("results :", data)
      data.forEach(el=>{
        this.manufactureService.getManufature.push();
    })
    },error => {
      console.log("Error :", error)
    });
  }
}

服务功能:

// import { HttpClient } from '@angular/common/http';

constructor(private http: HttpClient) {} 

getManufature(){
    return this.http.get('path or url to JSON'); 
}