0

could you please tell how to call http call in angular 2 and display data using ng-repeat ?

here is my code http://plnkr.co/edit/u6LXrvGuC6f3bOT1tsaZ?p=preview

import {Component,View} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {Router} from 'angular2/router';

@Component({

    templateUrl: 'home/home.html'
})


export class AppComponent {
   toDoModel;
  constructor(private _router:Router) {
   http.get('data.json')
      .map(res => res.json())

  }

  onclck(inputValue){
    alert(inputValue)
    this._router.navigate(['Second']);
  }

}

2 Answers 2

3

First you need to inject an Http instance into you component:

export class AppComponent {
  toDoModel;

  constructor(private _router:Router,private http:Http) {
    this.http.get('data.json')
        .map(res => res.json())
  }
}

There are then two ways to display your data in an ngFor:

  • By subscribing on the observable returned by the get method

    @Component({
      template: `
        <ul>
          <li *ngFor="#elt of elements">{{elt.name}}</li>
        </ul>
      `
    })
    export class AppComponent {
      toDoModel;
    
      constructor(private _router:Router,private http:Http) {
        this.http.get('data.json')
            .map(res => res.json())
            .subscribe(data => {
              this.elements = data;
            });
      }
    }
    
  • By leveraging the async pipe

    @Component({
      template: `
        <ul>
          <li *ngFor="#elt of elements | async">{{elt.name}}</li>
        </ul>
      `
    })
    export class AppComponent {
      toDoModel;
    
      constructor(private _router:Router,private http:Http) {
        this.elements = this.http.get('data.json')
            .map(res => res.json());
      }
    }
    

Don't forget to specify HTTP_PROVIDERS when bootstrapping your application:

import {bootstrap} from 'angular2/platform/browser';
import {HTTP_PROVIDERS} from 'angular2/http';
import {AppComponent} from './app.component';

bootstrap(AppComponent, [ HTTP_PROVIDERS ]);

Here is the corresponding plunkr: https://plnkr.co/edit/bdFeiiAqrPDtFUdguw7s?p=preview.

You could also put your HTTP processing into a dedicated service as described below:

Sign up to request clarification or add additional context in comments.

3 Comments

please share plunker
Regarding your plunkr, you forgot to add the http.dev.js file in your HTML file and add import 'rxjs/add/operator/map'; in your script.js file. See this answer for more details: stackoverflow.com/questions/34515173/…
@ThierryTemplier Thank you! For me, subscribe was the key. I wasn't subscribing to the result because I didn't care about the result--I just wanted to fire and forget. But without the subscription, it never fires!
0

You need it "inject" Http in order to use it

constructor(private http: Http){
  http.get('https://...')
    .map(response  => { response.json(); })
    .subscribe(value => { this.data = value;})  
}

1 Comment

Injecting and importing are different things - you need both. See the constructor parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.