如何使用http 2中的http get调用从json文件中获取值?

时间:2016-11-24 11:33:19

标签: angular

enter image description here我试图使用http get调用从我的json文件中获取值,但是收到错误..

这是我的component.ts文件

import {Component} from '@angular/core';
import {MyListComponent} from "./service-list.component";

    @Component({
        selector: 'my-app',
        template: `
        <country-list></country-list>
        `,

    })
    export class AppComponent {
    }

这是另一个组件

import {Component , OnInit} from "@angular/core";
import {CountryService} from "./country.service";
import {Country} from "./country";


@Component({
   selector: "country-list",
   template: ` List of Countries<br>
   <ul>
      <li *ngFor="let countrie of countries">{{ countrie.name }}</li>
   </ul>
   `,
   providers: [CountryService]
})

export class MyListComponent implements OnInit {
   contacts
   constructor(private _countryService: CountryService) {}

   ngOnInit(){
      this._countryService.getContacts()
      .subscribe(contacts => this.contacts = contacts)
   }
}

这是我的服务文件

import {Injectable} from "@angular/core";
import {Http , Response} from  '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class CountryService {
    constructor(private http:Http){}
   getContacts() {
    return this.http.get('app/contacts.json')
     .map((res: Response)=> res.json())

   }
}

我是棱角分明的新手2请帮帮我

0 个答案:

没有答案