typescript /Angular 2 : Cast JSON to interface with Observable & JSONP

标签 typescript angular jsonp rxjs observable

我想将我的 json 数组转换为我创建的界面,并希望在浏览器中显示它。我认为我的界面可能有问题,但我无法弄清楚...我需要更改什么才能让我的代码运行?

接口(interface):

 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",
}]

编辑

  1. 我已经在我的 chrome 网络选项卡中检查了请求是否正确并且它正常工作:200 OK --> 响应也很好
  2. 我按照蒂埃里的说法编辑了我的代码,现在它终于显示了我数组中的第一个对象:-)!!但我现在收到以下错误:

Uncaught EXCEPTION: Error in app/html/app.html:27:11 ORIGINAL EXCEPTION: RangeError: Provided date is not in valid range. ORIGINAL STACKTRACE: RangeError: Provided date is not in valid range. at boundformat (native) at Function.DateFormatter.format (http://localhost:3000/node_modules/@angular/common/src/facade/intl.js:100:26) at DatePipe.transform (http://localhost:3000/node_modules/@angular/common/src/pipes/date_pipe.js:25:37) at eval (http://localhost:3000/node_modules/@angular/core/src/linker/view_utils.js:188:22) at DebugAppView._View_AppComponent1.detectChangesInternal (AppComponent.template.js:377:148) at DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core/src/linker/view.js:200:14) at DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core/src/linker/view.js:289:44) at DebugAppView.AppView.detectContentChildrenChanges (http://localhost:3000/node_modules/@angular/core/src/linker/view.js:215:37) at DebugAppView._View_AppComponent0.detectChangesInternal (AppComponent.template.js:198:8) at DebugAppView.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core/src/linker/view.js:200:14) ERROR CONTEXT: [object Object]

最佳答案

您可以尝试以下方法:

this.videoData = this.jsonp
    .get('localhost:8080/video/newst/' + count +
                      '?jsonp=JSONP_CALLBACK')
            .map(res => <Video[]>res.json();

编辑

我认为您的请求不会返回 JSONP 内容,而是返回经典内容 (JSON)。如果是这样,您可以尝试以下操作:

import { bootstrap }  from 'angular2/platform/browser';
import { Component } from 'angular2/core';
import { HTTP_PROVIDERS, Http } from 'angular2/http';
import "rxjs/add/operator/map";

@Component({
  selector: "app",
  templateUrl: "app.html",
  providers: [HTTP_PROVIDERS]
})
class App {
  private feedData: Observable<Video[]>;

  constructor(private http: Http) { }

  ngOnInit() {
    this.displayNewstVideo(10);
  }

  private displayNewstVideo(count: number) {
    this.videoData = this.http
      .get('localhost:8080/video/newst/' + count)
      .map(res => (res.json() as Video[]))
      .do(videoData => {
        console.log(videoData);
      });
  }
}

bootstrap(App);

关于 typescript /Angular 2 : Cast JSON to interface with Observable & JSONP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37212948/

相关文章:

angular - *ngFor 在 Angular 2 中不起作用?

javascript - 如何跟踪 Angular 中一个组件中另一个组件中发出的值的变化

javascript - TypeScript 闭包范围错误 : The name does not exist in the current scope

Angular 为多个根路径重用相同的延迟加载模块

unit-testing - 测试组件时如何模拟管道

jquery - 使用 jQuery 访问远程 Web 服务

json - 覆盖 ExtJS 4 Store 中的默认参数名称

node.js - Dall E API 错误 : "Invalid input image - format must be in [' RGBA', 'LA' , 'L' ],得到 RGB。”

javascript - 如何在 requireJS 中加载 JSONP?

angularjs - Angular2 中的 ui-select 替换