Building Single Page Application Using Angular 11 — FrontEnd

Sunil Kumar
4 min readNov 22, 2020

--

In this post, we will focus on building the Angular frontend integrating with WebApi

Series Links

Building an ASP.NET Core WebAPI using Clean Architecture

Building an Angular 11 Application integrated with WebAPI

Overview

Building Angular Single Page Application integrating with backend ASP.NET WebAPI.

Setting up Angular 11 for Local Development

To install angular the following pre-requisites installations is required

NodeJs active LTS

NPM package Manager

Installing the Angular CLI

The Angular CLI is a command-line interface tool that you use to initialize, develop, scaffold, and maintain Angular applications.

npm install -g @angular/cli

Verify the version of angular using the below command

ng — version

Angular Version installed in my local development environment

Creating an Angular App

Run the CLI command ng and provide the name of the app as shown below

ng new myapp

Run the App

To run the application navigate to the app folder created and run the below command

cd myapp

npm install

ng serve — open

The ng serve command launches the server, watches your files, and rebuilds the app as you make changes to those files.

In this application, we are going to use the ngbootstrap CSS. To install run the below command in the terminal

npm install — save @ng-bootstrap/ng-bootstrap

Angular App Structure

Using visual studio code editor for building the application. Below is the application structure

Project code is available on Github

Creating project folders

Inside the “app” folder, let’s create the folders “_models”, “_components”, “_shared” “_services”.
In the “_models” folder we will place our entities, like User:

Creating Components

Components are the main building block for Angular applications. Each component consists of:

  • An HTML template that declares what renders on the page
  • A Typescript class that defines the behavior
  • A CSS selector that defines how the component is used in a template Optionally, CSS styles applied to the template

Sample Login Component

Services

Services can be used to have business logic in one place and can be injected into components when required.

Dependency Injection in Angular

Dependency injection (DI), is an important application design pattern. Angular has its own DI framework, which is typically used in the design of Angular applications to increase their efficiency and modularity.

To create a service, run the below command in the terminal window

ng generate service _services/authentication

Below is the sample Service

Interceptors

Intercepting requests and responses, With interception, you declare interceptors that inspect and transform HTTP requests from your application to a server. The same interceptors can also inspect and transform a server’s responses on their way back to the application. Multiple interceptors form a forward-and-backward chain of request/response handlers.

In our application, We have created JwtInteceptor to add the JWT token to every request that hits to web API. Below is the code implementation.

Read More about HttpClient in Angular documentation.

Global Error Handler

Handling exceptions in common service that implements ErrorHandler.

AuthGuard

AuthGuard can be used to restrict the user from viewing unauthorized pages or Components. Below is the AuthGuard Implementation.

Running the Application in Visual Studio Code

Open the project folder in the visual studio code and click on the run tab and select the projects to run as shown in the below screenshot

Angular Application and WebAPI are running in URL's below

WebAPI: http://localhost:5001

AngularWeb: http://localhost:5003

Test User to Login

  • Username: system
  • Password: admin@123

Conclusion

Hope you enjoyed the post and please feel free to play around with code

--

--

Sunil Kumar
Sunil Kumar

Written by Sunil Kumar

Architect and Full Stack Developer in Dotnet Core, C#, WebApi’s, Integrations and Learner on Angular and React FrontEnd

Responses (1)