【发布时间】:2016-09-09 19:47:23
【问题描述】:
我想将我的 json-array 转换为我创建的界面,并希望在浏览器中显示它。我认为我的界面可能有问题,但我无法弄清楚... 我需要更改什么才能让我的代码运行?
界面:
export interface Video {
id: number;
name: string;
description: string;
createdAt: string;
}
app.ts
import {JSONP_PROVIDERS, Jsonp} from '@angular/http';
import {Observable} from '../../../node_modules/rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/share';
import {Video} from './networking/api';
private videoData: Observable<Video[]>;
ngOnInit() {
this.displayNewstVideo(10);
}
private displayNewstVideo(count: number) {
this.videoData = this.jsonp
.get('localhost:8080/video/newst/' + count + '?jsonp=JSONP_CALLBACK')
.map(res => (res.json() as Video[]));
alert(this.videoData.count);
}
app.html
<div class="container">
<div class="video" style="font-family:sans-serif" *ngFor="#entry of videoData | async; #i = index">
<br *ngIf="i > 0" />
<span class="title" style="font-size:1.2rem">
<span>{{i + 1}}. </span>
<a href={{entry.urlDesktop}}>{{entry.name}}</a>
</span>
<span> ({{entry.description}})</span>
<div>Submitted at {{entry.createdAt * 1000 | date:"mediumTime"}}.</div>
</div>
JSON
[{
id: 1,
name: "Some Name",
description: "BlaBla",
createdAt: "2016-05-04 13:30:01.0",
},
{
id: 2,
name: "Some Name",
description: "BlaBla",
createdAt: "2016-05-04 13:30:01.0",
}]
编辑
- 我已经检查了我的网络选项卡中的请求是否正确,并且它是否正常工作:200 OK --> 响应也很好
- 我按照 Thierry 的说明编辑了我的代码,现在它终于显示了我的数组中的第一个对象 :-)!!但我现在收到以下错误:
未捕获的异常:app/html/app.html:27:11 中的错误 原始异常: RangeError:提供的日期不在有效范围内。 原始堆栈跟踪: RangeError:提供的日期不在有效范围内。 在 boundformat (本机) 在 Function.DateFormatter.format (http://localhost:3000/node_modules/@angular/common/src/facade/intl.js:100:26) 在 DatePipe.transform (http://localhost:3000/node_modules/@angular/common/src/pipes/date_pipe.js:25:37) 在评估 (http://localhost:3000/node_modules/@angular/core/src/linker/view_utils.js:188:22) 在 DebugAppView._View_AppComponent1.detectChangesInternal (AppComponent.template.js:377:148) 在 DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core/src/linker/view.js:200:14) 在 DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core/src/linker/view.js:289:44) 在 DebugAppView.AppView.detectContentChildrenChanges (http://localhost:3000/node_modules/@angular/core/src/linker/view.js:215:37) 在 DebugAppView._View_AppComponent0.detectChangesInternal (AppComponent.template.js:198:8) 在 DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core/src/linker/view.js:200:14) 错误上下文: [对象对象]
【问题讨论】:
标签: typescript angular jsonp rxjs observable