最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 http.get code not working - Stack Overflow

programmeradmin1浏览0评论

I am trying out this code in Angular2 beta 15.

import {Component} from 'angular2/core';
import {Http, Response} from 'angular2/http';

@Component({
    selector: 'my-app',
    template: `
      <h2>Basic Request</h2>
      <button type="button" (click)="makeRequest()">Make Request</button>
      <div *ngIf="loading">loading...</div>
      <pre>{{data.title}}</pre>

    `

})
export class AppComponent {

    data: any;
    loading: boolean;

    constructor(public http: Http) {
    }
    makeRequest(): void {
        this.loading = true;
        this.http.get('')
            .map((res: Response) => res.json())
            .subscribe(res => {
                this.data = res;
                this.loading = false;
            });
    }

}

For some reason it's getting the data but not displaying it.

What I'm I missing here?

I am trying out this code in Angular2 beta 15.

import {Component} from 'angular2/core';
import {Http, Response} from 'angular2/http';

@Component({
    selector: 'my-app',
    template: `
      <h2>Basic Request</h2>
      <button type="button" (click)="makeRequest()">Make Request</button>
      <div *ngIf="loading">loading...</div>
      <pre>{{data.title}}</pre>

    `

})
export class AppComponent {

    data: any;
    loading: boolean;

    constructor(public http: Http) {
    }
    makeRequest(): void {
        this.loading = true;
        this.http.get('http://jsonplaceholder.typicode./photos/1')
            .map((res: Response) => res.json())
            .subscribe(res => {
                this.data = res;
                this.loading = false;
            });
    }

}

For some reason it's getting the data but not displaying it.

What I'm I missing here?

Share asked Apr 20, 2016 at 19:59 Satch3000Satch3000 49.5k90 gold badges225 silver badges349 bronze badges 2
  • does your loading property get updated properly? – Kevin B Commented Apr 20, 2016 at 20:36
  • It just says Loading... and the response is null – Satch3000 Commented Apr 20, 2016 at 20:38
Add a ment  | 

2 Answers 2

Reset to default 5

My code works fine on "angular2": "2.0.0-beta.15" :

import {Component} from 'angular2/core';
import {HTTP_PROVIDERS,Http,Response} from 'angular2/http';
// see issue more detail here : https://github./angular/angular/issues/5632#issuement-167026172
import 'rxjs/Rx';



@Component({
    selector: 'my-app',
    template: `
      <h2>Basic Requests</h2>
      <button type="button" (click)="makeRequest()">Make Request</button>
      <div *ngIf="loading">loading......</div>
      <pre>title : {{array.title}}</pre>

    `,
    providers: [HTTP_PROVIDERS]
}
export class AppComponent {

    array = Array<any>;
    loading: boolean;

    constructor(
        public http:Http
    ){

    }
    makeRequest(): void {
        this.loading = true;
        this.http.get('http://jsonplaceholder.typicode./photos/1')
        .map((res: Response) => res.json())
            .subscribe(res => {
                this.array = res;
                this.loading = false;
            });

    }

}
  • Import import 'rxjs/Rx' see issue more detail here : https://github./angular/angular/issues/5632#issuement-167026172
  • Import import 'HTTP_PROVIDERS'
  • Add providers: [HTTP_PROVIDERS]

Everything looks ok, just change to:

{{data?.title}}

data is undefined until your request pletes. elvis operator helps to avoid null pointer.

Also add missing import:

import 'rxjs/Rx';
发布评论

评论列表(0)

  1. 暂无评论