Exploring Angular Fundamental With Visual Studio 2017

This article explains the fundamentals of Angular by creating a simple Angular application with Visual Studio 2017 using the default template available with .NET Core.

In this article, the following concepts have been covered.

  1. Creating an Angular App with .NET Core using Visual Studio 2017
  2. Adding component files manually.
  3. Adding component files by using inbuilt template and customization.
  4. A faster way to add a file in Visual Studio.
  5. Routing
  6. Data Binding
  7. Display JSON data from a file.
  8. Use of *ngIf & *ngFor .
  9. Apply Paging using “ngx-pagination”.
  10. Installing npm package with Visual Studio 2017

    1. Installfing an npm package through Quick package install.
    2. Installing an npm package through “Open Command Line”.
    3. Getting an npm package by making changes in the “package.json” file.

  1. Getting Data from an API Service

Creating an Angular Project with .NET Core using Visual Studio 2017

Step 1

Open Visual Studio 2017.

Angular Project

Step 2

Go to File >> New >> Project… (Ctrl + Shift + N).

Step 3

Select “ASP.NET Core Web Application”.

Select “ASP.NET Core Web Application” template in the “New Project” window and provide other details, as shown in the following screenshot and click OK.

Angular Project

Step 4 - Select Angular Template

Angular Project

It will take a while to create a new Angular app. Following is the screenshot of the newly created Angular app with .NET Core.

Angular Project

Step 5 - Run the application

Run the application with Google Chrome as the default browser.

Angular Project

You may see the debugging window for a while as shown in the preceding screenshot because Visual Studio 2017 Support JavaScript/TypeScript debugging. You can explore more about JavaScript and Typescript debugging in the following article,

After a while, you will see the running window with the home page. Following is the screenshot of the same.

Angular Project

You can explore “Counter” and “Fetch data” pages as well.

Routing

You have seen that the default application has been created as a small working example along with essential features like routing, data binding, calling a .NET Core API service from Angular App and other things have been implemented already. If you would like to explore, then you can explore this application by yourself as well. However, if you are new to Angular, then it will take some time to explore it.

I just want to start with the simplest one for routing rather than mixing the concepts. So just add a simple page for a calculator which does not require any dummy data from JSON file or data from API Service.

Add a new component manually.

Add a new folder “calculate” and 3 files inside this folder

  1. component.html
  2. component.ts
  3. component.css

Following is the screenshot of the same.

Angular Project

Faster way to add files in Visual Studio

If you would like to make file adding process faster then you can use “Add New File” extension which can be downloaded from here. After that just Press Shift +F2 to add a new file. Following are few screenshots showing that how faster we can add a file (or file with a folder) in Visual Studio.

Angular Project

Angular Project

Content of “~\app\components\calculate\calculate.component.ts”

  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.     selector: 'calculate',  
  5.     templateUrl: './calculate.component.html',  
  6.     styleUrls: ['./calculate.component.css']  
  7. })  
  8. export class CalculateComponent {       
  9. }  

Content of “~\app\components\calculate\calculate.component.html”

  1. <h1>Calculator Using Angular & TypeScript</h1>  
  2.   
  3. <br />  
  4. <div>  
  5.     <div class="row">  
  6.         <div class="col-sm-4">  
  7.             <label>First Number: </label>  
  8.         </div>  
  9.         <div>  
  10.         </div>  
  11.         <div class="col-sm-4">  
  12.             <input type="number" placeholder="Enter First Number">  
  13.         </div>  
  14.     </div>  
  15.     <br />  
  16.     <div class="row">  
  17.         <div class="col-sm-4">  
  18.             <label>Second Number: </label>  
  19.         </div>  
  20.         <div class="col-sm-4">  
  21.             <input type="number" placeholder="Enter Second Number">  
  22.         </div>  
  23.     </div>  
  24.     <br />  
  25.     <div class="row">  
  26.         <div class="col-sm-4">  
  27.             <label>Result: </label>  
  28.         </div>  
  29.         <div class="col-sm-4">  
  30.         </div>  
  31.     </div>  
  32.     <br />  
  33.   
  34.     <div class="row">  
  35.         <div class="col-sm-3">  
  36.   
  37.         </div>  
  38.         <div class="col-sm-2">  
  39.             <button type="button" class="btn btn-primary">Add (∑)</button>  
  40.         </div>  
  41.         
  42.         <div class="col-sm-2">  
  43.             <button type="button" class="btn btn-primary">Sub (−)</button>  
  44.         </div>  
  45.          
  46.         <div class="col-sm-2">  
  47.             <button type="button" class="btn btn-primary">Mult (∗)</button>  
  48.         </div>  
  49.   
  50.         <div class="col-sm-3">  
  51.         </div>  
  52.     </div>  
  53.     </div>  

Content of “~\app\components\calculate\calculate.component.css”.

  1. .btn-primary {  
  2.     color: #fff;  
  3.     background-color: #0495c9;  
  4.     border-color: #357ebd;  
  5. }  
  6.   
  7.     .btn-primary:hover, .btn-primary:focus, .btn-primary:active, .btn-primary.active, .open > .dropdown-toggle.btn-primary {  
  8.         color: #fff;  
  9.         background-color: #1704c9;  
  10.         border-color: #285e8e;   
  11.     }  

Go to the file “~\app\app.shared.module.ts” and make the following changes

  1. add the following line in the import section
    import { CalculateComponent } from './components/calculate/calculate.component';

  2. inside declarations section add
    CalculateComponent

  3. inside forRoot([]) add
    { path: 'calculate', component: CalculateComponent }

Following is the screenshot of the same.

Angular Project

Add following line in the file “…\app\components\navmenu\navmenu.component.html”.

  1. <li [routerLinkActive]="['link-active']">  
  2.            <a [routerLink]="['/calculate']">  
  3.              <span class='glyphicon glyphicon-th-list'></span> Calculate  
  4.            </a>  
  5. </li>  

Following is a screenshot of the same.

Angular Project

Run the application. Once application launched to go to the Calculate page.

Angular Project

Finally, the routing task has been done, and we can navigate and access newly added page. However, if I will try to do the calculation on the newly added page, then we would not be able to do that. Because till now I have not added any data binding logic means nothing has been done yet to pass data from Component to Template and vice versa.

Data Binding

Data binding is an essential feature of any application and Angular provides multiple ways of data binding. Data binding a broad discussion topic and cannot be covered in a small section. So right now, I am going give brief about it.

Let’s have a quick look at the Angular data binding.

There are 4 different ways of data binding in Angular

  1. Interpolation
  2. Property Binding
  3. Event Binding
  4. Two-way Binding

Interpolation

Interpolation is one of the most popular ways of data binding. If you are coming from AngularJS background or C#, then you must have used interpolation already. In the same way, we can use interpolation in Angular. Interpolation is used to pass data from component to template or evaluate the inline expressions.

Syntax

{{Expression to evaluate}}

Example

Result: {{result}}

Property Binding

Property binding is used to pass data from component to template. It can be used with property, style, CSS class, style and so forth.

Syntax

[target] = "expression"

Example

  1. <span [innerText]="greet"></span> : {{userName}}  
  2. <input type="text" [value]="userName" readonly />  

Event Binding

Syntax

(target) = "statement"

Example

<button type="button" (click)="add()"> Add </button>

Two-way Binding

Syntax

[(target)] = "expression"

Example

<input [(ngModel)]="firstNumber" type="number" placeholder="Enter First Number">

Adding calculation logic & data binding to calculate component

Code changes for calculate.component.ts

Go back to calculator page. Open file “calculate.component.ts” and write following line inside CalculateComponent class.

  1. public firstNumber: number;  
  2.     public secondNumber: number;  
  3.     public result: number;  
  4.   
  5.     public add() {  
  6.         this.result = this.firstNumber + this.secondNumber;  
  7.     }  
  8.   
  9.     public sub() {  
  10.         this.result = this.firstNumber - this.secondNumber;  
  11.     }  
  12.   
  13.     public mult() {  
  14.         this.result = this.firstNumber * this.secondNumber;  
  15.     }  

Complete code for calculate.component.ts

  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.     selector: 'calculate',  
  5.     templateUrl: './calculate.component.html',  
  6.     styleUrls: ['./calculate.component.css']  
  7. })  
  8. export class CalculateComponent {  
  9.   
  10.     public firstNumber: number;  
  11.     public secondNumber: number;  
  12.     public result: number;  
  13.   
  14.     public add() {  
  15.         this.result = this.firstNumber + this.secondNumber;  
  16.     }  
  17.   
  18.     public sub() {  
  19.         this.result = this.firstNumber - this.secondNumber;  
  20.     }  
  21.   
  22.     public mult() {  
  23.         this.result = this.firstNumber * this.secondNumber;  
  24.     }      
  25. }  

Code changes for calculate.component.html

Open the file “calculate.component.html” and change the code to implement Two-way binding for first and second number.

  1. <input [(ngModel)]="firstNumber" type="number" placeholder="Enter First Number">  
  2. <input [(ngModel)]="secondNumber" type="number" placeholder="Enter Second Number">  

and use string interpolation for displaying the result

<label>{{result}} </label>

Add Event binding code for add, subtract and multiply button.

  1. <button type="button" (click)="add()" class="btn btn-primary"> Add (∑)</button>  
  2. <button type="button" (click)="sub()" class="btn btn-primary"> Sub (−)</button>  
  3. <button type="button" (click)="mult()" class="btn btn-primary"> Mult (∗)</button>  

Complete code for calculate.component.html

  1. <h1>Calculator Using Angular & TypeScript</h1>  
  2.   
  3. <br />  
  4. <div>  
  5.     <div class="row">  
  6.         <div class="col-sm-4">  
  7.             <label>First Number: </label>  
  8.         </div>  
  9.         <div>  
  10.         </div>  
  11.         <div class="col-sm-4">  
  12.             <input [(ngModel)]="firstNumber" type="number" placeholder="Enter First Number">  
  13.         </div>  
  14.     </div>  
  15.     <br />  
  16.     <div class="row">  
  17.         <div class="col-sm-4">  
  18.             <label>Second Number: </label>  
  19.         </div>  
  20.         <div class="col-sm-4">  
  21.             <input [(ngModel)]="secondNumber" type="number" placeholder="Enter Second Number">  
  22.         </div>  
  23.     </div>  
  24.     <br />  
  25.     <div class="row">  
  26.         <div class="col-sm-4">  
  27.             <label>Result: </label>  
  28.         </div>  
  29.         <div class="col-sm-4">  
  30.             <label>{{result}} </label>  
  31.         </div>  
  32.     </div>  
  33.     <br />  
  34.   
  35.     <div class="row">  
  36.         <div class="col-sm-3">  
  37.   
  38.         </div>  
  39.         <div class="col-sm-2">  
  40.             <button type="button" (click)="add()" class="btn btn-primary">Add (∑)</button>  
  41.         </div>  
  42.   
  43.         <div class="col-sm-2">  
  44.             <button type="button" (click)="sub()" class="btn btn-primary">Sub (−)</button>  
  45.         </div>  
  46.   
  47.         <div class="col-sm-2">  
  48.             <button type="button" (click)="mult()" class="btn btn-primary">Mult (∗)</button>  
  49.         </div>  
  50.   
  51.         <div class="col-sm-3">  
  52.         </div>  
  53.     </div>  
  54. </div>  

Run the application and calculate

Now rerun the application. Following is the screenshot of a running application, as you can see that now calculation logic is working fine. I have done summation; you can test it for subtraction and multiply as well.

Angular Project

Add a new component using the Angular Item templates option available in Visual Studio 2017.

Previously, you have seen that I have added calculate component, similarly add another component “customer”.

This time, I will not be adding a new component manually. I will add it using a template. Let’s have a look how we can add it using a template.

Right click on “component” folder >> Add >> New Item…

or you can use the keyboard shortcut “Ctrl + Shift + A”.

Angular Project

Check if “Angular Item Templates” is already installed or not.

Angular Project

If not, then please install it and re-open this window after installation.

Navigate to Installed >> Visual C# >> ASP.NET Core >> Web >> Angular >> Select Angular Component. Provide component name and click on Add button.

Angular Project

After that, it will open a new “Add new Angular Component” window.

Angular Project

It uses the default naming convention and provides multiple checkboxes for the choice of the Folder name, template name, class file name, test file name, style file name. Drop-Down list for stylesheet languages like CSS, SCSS, and LESS.

If you want a customized name, then you can provide the customized name as well for that just checked in the “Use custom names” checkbox. Following is a screenshot of the same.

Angular Project

Click on Add to proceed. It will take a while, and after that, it will add all the required files. Go to solution explorer and open the customer folder. You can see all the newly added files along with customer folder.

Newly Added Component

Angular Project

Add navigation and routing data.

Changes for routing in app.shared.module.ts

Go to the file app.shared.module.ts and make changes as shown in the following screenshot.

Angular Project

Changes for Navigation in navmenu.component.html

Open the file navmenu.component.html and add following lines

  1. <li [routerLinkActive]="['link-active']">  
  2.                     <a [routerLink]="['/customer']">  
  3.                         <span class='glyphicon glyphicon-user'></span> Customer  
  4.                     </a>  
  5.                 </li>  

Angular Project

Now run the application and click on the customer link provided in the left menu.

Angular Project

You can see that it is displaying the default generated text which proves that navigation and routing functionalities are working fine.

Display JSON data from a *.ts file

You can see that Navigation and routing are working fine for customer component and now for the newly added customer component we will display the data from a *.ts file. Means I am going to display the dummy data rather than calling an API service to get real data from a database.

  1. Add a class “customer.ts”

    Angular Project

  2. Add a new file “customerdata.ts”

    Angular Project

  3. Open “customer.ts” file and add properties inside the customer class
    1. export class customer {  
    2.     id: number;  
    3.     firstName: string;  
    4.     lastName: string;  
    5.     email: string;  
    6.     gender: string;  
    7.     city: string;  
    8.     country: string;  
    9. }  
  4. Open “customerdata.ts” file and import customer and add some dummy data.
    1. import { customer } from '../class/customer';  
    2.   
    3. export const CUSTOMERS: customer[] = [  
    4.         { "id": 1, "firstName""Bobbette""lastName""Wignall""email""[email protected]""gender""Female""city""Bochum""country""Germany" },  
    5.         { "id": 2, "firstName""Christine""lastName""ducarme""email""[email protected]""gender""Female""city""Makariv""country""Ukraine" },  
    6.         { "id": 3, "firstName""Geralda""lastName""Byart""email""[email protected]""gender""Female""city""Thị Xã Lai Châu""country""Vietnam" },  
    7.         { "id": 4, "firstName""Brynna""lastName""Couronne""email""[email protected]""gender""Female""city""Ciudad del Este""country""Paraguay" },  
    8.         { "id": 5, "firstName""Nikoletta""lastName""Hatchette""email""[email protected]""gender""Female""city""Utmānzai""country""Pakistan" },  
    9.         { "id": 6, "firstName""Darell""lastName""Foxhall""email""[email protected]""gender""Female""city""Daqiao""country""China" },  
    10.         { "id": 7, "firstName""Glynda""lastName""McCrystal""email""[email protected]""gender""Female""city""Izumi""country""Japan" },  
    11.         { "id": 8, "firstName""Britteny""lastName""Broy""email""[email protected]""gender""Female""city""Mazhu""country""China" },  
    12.         { "id": 9, "firstName""Milt""lastName""Riceards""email""[email protected]""gender""Male""city""Cavalões""country""Portugal" },  
    13.         { "id": 10, "firstName""Josselyn""lastName""Kinlock""email""[email protected]""gender""Female""city""Wulan""country""China" },  
    14.         { "id": 11, "firstName""Erick""lastName""Gheeraert""email""[email protected]""gender""Male""city""Heshe""country""China" },  
    15.         { "id": 12, "firstName""Riobard""lastName""Tarrant""email""[email protected]""gender""Male""city""Besisahar""country""Nepal" },  
    16.         { "id": 13, "firstName""Langsdon""lastName""Gerardi""email""[email protected]""gender""Male""city""Cikaso""country""Indonesia" },  
    17.         { "id": 14, "firstName""Thaine""lastName""Brann""email""[email protected]""gender""Male""city""Nizhneivkino""country""Russia" },  
    18.         { "id": 15, "firstName""Collie""lastName""Dewes""email""[email protected]""gender""Female""city""Rettikhovka""country""Russia" },  
    19.         { "id": 16, "firstName""Donny""lastName""Ingleson""email""[email protected]""gender""Female""city""Villasis""country""Philippines" },  
    20.         { "id": 17, "firstName""Vinita""lastName""Dowles""email""[email protected]""gender""Female""city""Santo Tomas""country""Philippines" },  
    21.         { "id": 18, "firstName""Bil""lastName""Rowan""email""[email protected]""gender""Male""city""Huangtian""country""China" },  
    22.         { "id": 19, "firstName""Yardley""lastName""Lambdean""email""[email protected]""gender""Male""city""Beisijiazi""country""China" },  
    23.         { "id": 20, "firstName""Renell""lastName""Valentinetti""email""[email protected]""gender""Female""city""Gayam""country""Indonesia" }  
    24. ]  
  5. Open “customer.component.html” file and write following HTML code inside this file.
    1. <h1>Customer Details</h1>  
    2.   
    3. <table class='table' *ngIf="customers">  
    4.     <thead>  
    5.         <tr>  
    6.             <th>Id</th>  
    7.             <th>Name</th>  
    8.             <th>Email</th>  
    9.             <th>Gender</th>  
    10.             <th>City</th>  
    11.             <th>Country</th>  
    12.         </tr>  
    13.     </thead>  
    14.     <tbody>  
    15.         <tr *ngFor="let customer of customers">  
    16.             <td>{{ customer.id}}</td>  
    17.             <td>{{ customer.firstName + " " + customer.lastName}}</td>  
    18.             <td>{{ customer.email }}</td>  
    19.             <td>{{ customer.gender }}</td>  
    20.             <td>{{ customer.city }}</td>  
    21.             <td>{{ customer.country }}</td>  
    22.         </tr>  
    23.     </tbody>  
    24. </table>  

Following is a screenshot of the same.

Angular Project

Have a look at the highlighted code in the following screenshot as I am going to explain some important concepts.

*ngIf

<table class='table' *ngIf="customers">

It will remove the entire element if provided expression become false. So, in the above scenarios if “customers” variable is null or undefined then it will hide the entire table.

*ngFor

<tr *ngFor="let customer of customers">

*ngFor is a repeater directive which repeats the host element for each element in the list. In the above example host element is <tr> so it will generate 20 <tr> because “customers” variable have 20 elements in the list.

Writing multiple elements in interpolation

<td>{{ customer.firstName + " " + customer.lastName}}</td>

As you can see that <td> for Name is using multiple variables and it has been concatenated which shows that we can use more than one element in interpolation.

Running the application and see the out after add dummy data

The task for adding dummy data for customers has been done, and now we can see the output on the UI screen so re-run the application if you have stopped it. If your application was running already just go to the browser and navigate to customer page, you do not need to re-build the application the web page should refresh and display the latest data automatically. Following is the screenshot of UI for customer page.

Angular Project

Apply Paging using “ngx-pagination”

You can see in the above screenshot that all data are being displayed but need to scroll down to see the complete data. Right now, we have only 20 customers, but in the live application, we may have hundreds of customers so we can use paging to display data.

There are multiple plugins available for Paging, and you can choose any one of then based on your requirements. Right now, I am going to use “ngx-pagination”.

Installing npm package with Visual Studio 2017

There are multiple ways to install an npm package with Visual Studio 2017, but I am going to explain only 3 favorite ways to install an NPM package with Visual Studio 2017.

Installing an NPM package through Quick package install.

In Visual Studio 2017 we have an option for installing “NuGet” & “Bower” packages by default as shown in the following screenshot.

Angular Project

However, there is no option provided by default to manage npm packages. However, we can add options for managing npm packages as well with Visual Studio 2017.

Download, a Visual Studio Extension, called “Package Installer” from here.

Angular Project

Download and install the package this package. Once it is completed, you can see a similar screen as shown below.

Angular Project

Re-open the application with Visual Studio 2017 >> Solution Explorer >> Select the Project >> Right click on it. Now you can see that there is an option available for “Quick Install Package…” or you can press the shortcut “Shift + Alt + 0”.

Angular Project

It will open the “Quick Install Package” window using which you can install npm, Bower, JSPM, NuGet, TSD, Typings and Yarn.

Angular Project

Installing an NPM package through “Open Command Line”.

Open Command Line is another option by using which we can install npm packages.

Go to Tool menu >> Extensions and Updates… >> search for Open Command Line

Angular Project

Download and install this extension. Once This installation is completed then re-open your project and go to “package.json” file. Right click on it and select “Open Command Line”. It will open a new command line window.

Angular Project

However, each time you do not need to go to package.json file and then open the command line window. You can open it by just using the shortcut “Alt + Space” from anywhere in the project.

Getting an NPM package By making changes in the package.json file.

We can install npm package through command line window, but I have noticed that many packages are failing to install correctly for Angular 4/5 while using it with Visual Studio 2017. There are some dependencies which have been renamed in Angular 4/5.

Go to “package.json”file and directly mention the name of the package which you would like to install and re-build it.

Angular Project

However, getting an npm package by editing “package.json” file is a manual process and must be aware that which package would like to keep in which place. You can see that most of the package name has been mentioned in "devDependencies" section whereas I have added "ngx-pagination" inside "dependencies" section.

Making changes for Pagination

Now re-build the application (it will restore npm packages automatically during the build) to check if everything is working fine.

Changes for app.shared.module.ts

Once "ngx-pagination" is installed make other required changes to run pagination properly.

  1. Open “app.shared.module.ts” and add following line
    1. import { NgxPaginationModule } from 'ngx-pagination';  
    add “NgxPaginationModule” inside “@NgModule.Imports” section.

    Angular Project

    Changes for customer.component.html

  2. Open “customer.component.html” and add following lines.
    1. <tr *ngFor="let customer of customers | paginate: { itemsPerPage: 10, currentPage: p }">  
    2. <pagination-controls (pageChange)="p = $event"></pagination-controls>  
    The changes have been highlighted in the screenshot as well.

    Angular Project

    Changes for customer.component.ts

  3. Open “customer.component.ts” and
    1. p: number = 1;  
    Angular Project

Refresh the application.

Angular Project

Getting Data from API Service

Till now we have added data by using a mock file, but now I am going make changes in the customer component to get data from API Service.

Writing code for Web API Controller “CustomerController”.

Create a Web API Controller “CustomerController” and now return data from API service

  1. using System.Collections.Generic;  
  2. using System.Linq;  
  3. using Microsoft.AspNetCore.Mvc;  
  4.   
  5. namespace AngularFundamental.Controllers  
  6. {  
  7.     [Produces("application/json")]  
  8.     [Route("api/Customer")]  
  9.     public class CustomerController : Controller  
  10.     {  
  11.         [HttpGet("[action]")]  
  12.         public IEnumerable<Customer> GetCustomers()  
  13.         {  
  14.             return Customers();  
  15.         }  
  16.   
  17.         private List<Customer> Customers()  
  18.         {  
  19.             //return customer  
  20.      //getting data              
  21.         }  
  22.     }  
  23.   
  24.     public class Customer  
  25.     {  
  26.         public int Id { get; set; }  
  27.         public string FirstName { get; set; }  
  28.         public string LastName { get; set; }  
  29.         public string Email { get; set; }  
  30.         public string Gender { get; set; }  
  31.         public string City { get; set; }  
  32.         public string Country { get; set; }        
  33.     }  
  34.       
  35. }  

Code Changes for customer.component.ts

Now go to the file “customer.component.ts” and change the code.

  1. Write below code in the import section
    1. import { Component, Inject } from '@angular/core';  
    2. import { Customer } from '../../class/customer';  
    3. import { Http } from '@angular/http';  
    I am writing “Inject”, “Customer” and “Http” in the import section because later on, I am going to use it.

    Angular Project

  2. Comment the below lines as no longer I am going to use “CUSTOMERS” constant.
    1. //import { CUSTOMERS } from '../../dummydata/customerdata'  
    2. //customers = CUSTOMERS;  
  3. Create a new array of type Customer.
    1. public customers: Customer[];  
  4. From the constructor call the API service.
    1. constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {  
    2.         http.get(baseUrl + 'api/Customer/GetCustomers').subscribe(result => {  
    3.             this.customers = result.json() as Customer[];  
    4.         }, error => console.error(error));  
    5.     }  

Complete code for customer.component.ts

  1. import { Component, Inject } from '@angular/core';  
  2. import { Customer } from '../../class/customer';  
  3. import { Http } from '@angular/http';  
  4. //import { CUSTOMERS } from '../../dummydata/customerdata'  
  5.   
  6.   
  7. @Component({  
  8.     selector: 'app-customer',  
  9.     templateUrl: './customer.component.html',  
  10.     styleUrls: ['./customer.component.css']  
  11. })  
  12. /** customer component*/  
  13. export class CustomerComponent {  
  14.   
  15.     p: number = 1;  
  16.     //customers = CUSTOMERS;  
  17.     public customers: Customer[];  
  18.     constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {  
  19.         http.get(baseUrl + 'api/Customer/GetCustomers').subscribe(result => {  
  20.             this.customers = result.json() as Customer[];  
  21.         }, error => console.error(error));  
  22.     }  
  23.   
  24. }  

Now re-run the application and navigate to the customer.

Angular Project

You can see that now, the data is coming from the API service and earlier we had only 20 records from a mock data, but right now, I am getting 50 records from an API service thus a total number of pages has been increased to 10 as we are displaying 5 records per page. In this application have used a very simple example for API service but if you would like to explore more about web API service or would like to write a well-designed API service, then please go through the below articles,

Up Next
    Ebook Download
    View all
    Learn
    View all