所以我试图从Api检索数据并且无法通过密钥选择检索
app / app.component.ts
24:00
服务文件
应用程序/ app.service.ts
import {Component} from 'angular2/core';
import {Http} from 'angular2/http';
import {httpServiceClass} from './service';
import {HTTP_PROVIDERS} from 'angular2/http';
import {Observable} from 'rxjs/Observable';
import {Api} from './api';
@Component({
selector: 'my-app',
templateUrl: 'mytemplate.html',
providers:[httpServiceClass,HTTP_PROVIDERS]
})
export class AppComponent implements OnInit{
private api:Api[];
constructor(private getservice:httpServiceClass){
}
ngOnInit(){
this.getservice.httprequest()
.subscribe(data => this.api = data );
}
}
api.ts
import {Injectable} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';
import {Api} from './api';
@Injectable()
export class httpServiceClass{
constructor(private http: Http){}
httprequest(): Observable<Api[]>{
return this.http.get(' http://date.jsontest.com')
.map(response => response.json());
}
}
mytemplate.html
export interface Api{
time:string;
date:string;
}
返回HTTP:{&#34; time&#34;:&#34; 01:52:41 PM&#34;,&#34; milliseconds_since_epoch&#34;:1472910761126,&#34; date&#34;: &#34; 2016年9月3日&#34; }
我尝试按键选择
<h1 >HTTP : {{api|json}}</h1>
在控制台中返回
例外:找不到不同的支持对象&#39; [object Object]&#39;在[AppComponent @ 0:4中的api]
答案 0 :(得分:0)
运行时的界面在javascript世界中没什么。尝试将您的界面更改为类,看看它是否解决了问题。然后相应地折射代码。