Arrays 从返回的http请求类型生成对象数组<;回应>;

Arrays 从返回的http请求类型生成对象数组<;回应>;,arrays,angular,http,typescript,angular-promise,Arrays,Angular,Http,Typescript,Angular Promise,我有一个服务,它请求从API服务器获取请求并返回json,我将其转换为Promise,下面是服务的代码 import { Injectable } from "@angular/core"; import { HttpClientModule } from "@angular/common/http"; import { HttpModule, Http } from "@angular/http"; import { Osoba } from "./osoba"; import "rxjs/a

我有一个服务,它请求从API服务器获取请求并返回json,我将其转换为Promise,下面是服务的代码

import { Injectable } from "@angular/core";
import { HttpClientModule } from "@angular/common/http";
import { HttpModule, Http } from "@angular/http";
import { Osoba } from "./osoba";
import "rxjs/add/observable/of";
import { Response } from "@angular/http/src/static_response";

@Injectable()
export class OsobaService {
  constructor(private http: Http) {}
  public getOsobe(): Promise<any> {
    return this.http.get("http://localhost:62066/api/values").toPromise();
  }
}
这是osobe的日志

ZoneAwarePromise{zone_symbol___状态:null,__zone_symbol___值:数组(0)} __区域符号状态 : 真的 __区域符号值 : 答复{正文:“[{“osobaID”:1,“ime”:“Aleksa”,“prezime”:“Jevtic…”,“prezime”:“Stevanovic”,“jmbg”:“2504996854112”}]”,状态:200,ok:true,状态文本:“ok”,标题:标题,…} __原型 : 反对

我尝试使用ngFor列出对象,但在html页面上没有得到任何响应

<ul><li *ngFor="let osoba of osobe">
{{osoba.ID}} {{osoba.ime}}
</li>
</ul>
    {{osoba.ID}{{osoba.ime}
如何从Promise创建数组?或者至少将承诺中的数据存储到列表中? 很抱歉,日志代码很难看,我不确定如何格式化它。

试试这个:

return this.http.get("http://localhost:62066/api/values").map(res => res.json());

您必须使用.json()创建API是正常的:


您必须在服务函数调用中添加then和catch,如:

   this.osobaService.getOsobe().then((response) => {console.log(response);}).catch((error) => {console.log(error); });

属性“map”在类型“Observable”上不存在。@АаСаааааааћ能否添加导入“rxjs/add/Observable/map”;在您的服务文件中?@Аааааааааааћ我现在得到了不同的日志,管理得更好,但即使在添加异步管道之后,我仍然无法列出它们,有什么想法吗?让我们来看看。
import "rxjs/add/observable/map"; //<-- add this import


@Injectable()
export class OsobaService {
  constructor(private http: Http) {}
  public getOsobe(): Promise<any> {
    return this.http.get("http://localhost:62066/api/values")
      .map(res => res.json()) //<-- here 
      .toPromise();
  }
}
export class TableViewComponent implements OnInit {
  osobe: Promise<any> //<-- add

  constructor(private osobaService: OsobaService) {}

  ngOnInit() {
    this.osobe = this.osobaService.getOsobe(); //<-- update
  }
}
<ul>
   <li *ngFor="let osoba of osobe | async">{{osoba.osobaID}} {{osoba.ime}}</li>
</ul>
   this.osobaService.getOsobe().then((response) => {console.log(response);}).catch((error) => {console.log(error); });