Typescript / Angular2:将JSON转换为与Observable和JSONP的接口

问题描述 投票:7回答:3

我想将我的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",
}]

编辑

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

未捕获的EXCEPTION:app / html / app.html中的错误:27:11原始异常:RangeError:提供的日期不在有效范围内。 ORIGINAL STACKTRACE:RangeError:提供的日期不在有效范围内。在位于DebugAppView.AppView.detectChanges的DebugAppView._View_AppComponent1.detectChangesInternal(AppComponent.template.js:377:148)的eval(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)的Function.DateFormatter.format(http://localhost:3000/node_modules/@angular/core/src/linker/view_utils.js:188:22)的boundformat(native)处( http://localhost:3000/node_modules/@angular/core/src/linker/view.js:200:14)位于DebugAppView.AppView.detectChanges(调试版)的DebugAppView.AppView.detectChanges内部(AppComponent.template.js:198:8)DebugAppView.AppView.detectChildrenChanges(http://localhost:3000/node_modules/@angular/core/src/linker/view.js:289:44)上的DebugAppView.detectChanges(http://localhost:3000/node_modules/@angular/core/src/linker/view.js:215:37)错误上下文:[object object] ]

typescript angular jsonp rxjs observable
3个回答
5
投票

您可以尝试以下方式:

http://localhost:3000/node_modules/@angular/core/src/linker/view.js:200:14

编辑

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

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

1
投票

尝试使用类而不是接口,所以在这种情况下video.model.ts将是:

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);

1
投票

我最近做了一个TypeScript的演示,这让我想起幻灯片的标题是什么“没有界面!”在TypeScript中定义export class Video { constructor( public id: number, public name: string, public description: string, public createdAt: string){} } 时,它实际上会编译为空。这可能有点误导。我想我明白你要做的是:

问题是JSON对象返回的方式,它是填充的。因此它位于指数qazxsw poi中。试试这个:

interface
© www.soinside.com 2019 - 2024. All rights reserved.