首页 文章

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

提问于
浏览
7

我创建了'd like to cast my json-array to my interface which I'并希望在浏览器中显示它 . 我认为我的界面可能有问题,但我无法弄明白......我需要更改什么来让我的代码运行?

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

Edits

  • 我已经检查了我的网络选项卡中的请求是否正确,并且它的工作情况如下:200 OK - >响应也很好

  • 我按照Thierry的说法编辑了我的代码,现在它终于显示了我的数组中的第一个对象:-) !!但我现在得到以下错误:

未捕获的EXCEPTION:app / html / app.html中的错误:27:11原始异常:RangeError:提供的日期不在有效范围内 . ORIGINAL STACKTRACE:RangeError:提供的日期不在有效范围内 . 在DatePipe.transform(http:// localhost:)的Function.DateFormatter.format(http:// localhost:3000/node_modules/@angular/common/src/facade/intl.js:100:26)的boundformat(native)处3000/node_modules/@angular/common/src/pipes/date_pipe.js:25:37)eval(http:// localhost:3000/node_modules/@angular/core/src/linker/view_utils.js:188:22 )在DebugAppView.AppView.detectChanges的DebugAppView._View_AppComponent1.detectChangesInternal(AppComponent.template.js:377:148)中(http:// localhost:3000/node_modules/@angular/core/src/linker/view.js:200: 14)在DebugAppView.AppView.detectContentChildrenChanges(http:// localhost:3000 / node_modules)的DebugAppView.detectChanges(http:// localhost:3000/node_modules/@angular/core/src/linker/view.js:289:44)处/@angular/core/src/linker/view.js:215:37)在DebugAppView.AppView.detectChanges(http:// localhost:3000 /)的DebugAppView._View_AppComponent0.detectChangesInternal(AppComponent.template.js:198:8)中node_modules/@angular/core/src/linker/view.js:200:14)错误上下文:[ob ject对象]

3 回答

  • 5

    您可以尝试以下方式:

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

    Edit

    我认为您的请求不会返回JSONP内容,而是返回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);
    
  • 1

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

    export class Video {
      constructor(
        public id: number,
        public name: string,
        public description: string,
        public createdAt: string){}
    }
    
  • 1

    我最近发表了一个关于 TypeScript 的演示文稿,这让我想起了幻灯片的 Headers "There is no interface!"在 TypeScript 中,当你定义一个 interface 时,它实际上编译为零 . 这可能有点误导 . 我想我明白你要做的是:

    问题是 JSONP 对象返回的方式,它是填充的 . 所以它位于索引 [1] . 试试这个:

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

相关问题