Before moving ahead, you need to configure your Angular 2 application. Follow these articles first:
Getting Started
• Start Visual Studio.
• Create a new website.
• Provide the name and the location of website.
• Click "Next".
Here is my cricketer Web API.

Let’s add a model component, provide an appropriate name, and add reference of model class and other important assemblies.
- export class Cricketer {
- constructor(
- ID: number,
- Name: string,
- ODI: number,
- Test: number
- ) { }
- }
Now, let’s add a new service component and provide name.
- import { Injectable } from '@angular/core';
- import { Http, Headers, RequestOptions, Response } from '@angular/http';
- import { Cricketer } from './Cricketer';
- import { Observable, Subject } from 'rxjs/Rx';
- import 'rxjs/Rx'; //get everything from Rx
- import 'rxjs/add/operator/toPromise';
- @Injectable()
- export class CricketerService {
- apiUrl: string = "http://localhost:51453/api/Cricketers";// Web API URL
- constructor(private _http: Http) { }
- private RegenerateData = new Subject<number>();
- RegenerateData$ = this.RegenerateData.asObservable();
- AnnounceChange(mission: number) {
- this.RegenerateData.next(mission);
- }
- // //
- // getCricketers() {
- // return this._http.get(this.apiUrl)
- // .map((response) => response.json());
- //}
- getCricketers(): Observable<Cricketer[]> {
- return this._http.get(this.apiUrl)
- .map((res: Response) => res.json())
- .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
- }
- }
- import { Component } from '@angular/core';
- import { CricketerService } from './shared/cricketerservice';
- import { Cricketer } from './shared/Cricketer';
- import { Observable } from 'rxjs/Rx';
- @Component({
- moduleId: module.id,
- selector: 'cricketer-component',
- templateUrl: 'cricketercomponent.html',
- providers: [CricketerService]
- })
- export class CricketerComponent {
- cricketers: Observable<any[]>;
- constructor(private _cricketerService: CricketerService) {
- }
- ngOnInit() {
- this.getCricketers();
- }
- //
- getCricketers() {
- debugger
- thisthis.cricketers = this._cricketerService.getCricketers();
- }
- }
- <div class="row">
- <div class="table-responsive">
- <table class="table ">
- <thead class="thead-default">
- <th>ID</th>
- <th>Player</th>
- <th>Total ODI</th>
- <th>Total Test</th>
- <th></th>
- </thead>
- <tbody>
- <tr *ngFor="let cric of cricketers | async" scope="row">
- <td>{{cric.ID}}</td>
- <td>{{cric.Name}}</td>
- <td>{{cric.ODI}}</td>
- <td>{{cric.Test}}</td>
- <td>
- <button type="button" value="Update" class="btn btn-default" (click)='editCricketer(cric.ID, cric)'>Update</button>
- <button type="button" value="Delete" class="btn btn-danger" (click)='deleteCricketer(cric.ID)'>Delete</button>
- </td>
- </tr>
- </tbody>
- </table>
- </div>
- <div class="col-md-offset-3 col-md-3">
- <a routerLink="/addcricketercomponent" class="btn btn-warning">Add Player</a>
- </div>
- </div>


Kamlesh KumarPosted Mar 10, 2019, 10:13 AM
What the hell is done incomplete article
Ketan ShindePosted Jan 16, 2018, 12:14 AM
This is not helpfull .....
DotNet TechyPosted Sep 14, 2017, 11:30 AM
Video Tutorial for consuming web api in angular 2 or 4.
Pankaja ShankarPosted Aug 19, 2017, 10:06 PM
What do I have to do to use relative url for the http request instead of giving localhost.../api/... it was easier with angularjs to hook up to webapi.
Anil KumarPosted Feb 2, 2017, 6:17 AM
Thank you for valuable article
Madhanmohan DevarajanPosted Feb 1, 2017, 8:58 PM
Very nicely described for easy understanding.