01 logo

Step by Step Guide to Create Angular App from Scratch

Web application developments

By Grace DobrevPublished about a year ago 7 min read
Like

Web application developments have now shown a remarkable growth in their demand. Due to stringent competition in the market, people are more and more inclining towards those web app development company which are amplifying their growth. Angular has been remarkable proved its monopoly of being the beast of the typescript based open-source app development framework. This fact has been undeniable from all the developers and is also takes place of being one of the mainstay web developments that are absolutely justified.

Let’s help our accidental bypassers to understand the Angular in a yet effortless manner.

Angular: Things You Need to Know

As mentioned above it is a TypeScript based open-source framework that makes your app development task easy. Nothing can beat Angular when it comes to single-page app development alongside other sorts of app. The community upholds more than 18 million users, and the number is not stagnant yet.

Another thing that is a commendable aspect of Angular is flexibility regarding server communication. It allows you to work with any server-side technology like other JavaScript MVC frameworks. The other tool that is equally great is Angular command-line interface. Recommended by many engineers, Angular CLI is quick to set up, easy to comprehend even for beginners, loaded with a variety of testing tools just out of the box, and its commands are nothing but direct and easy.

This was quite a brief introduction about Angular. Now it's time to push the train station further and understand step by step process to create an Angular application from scratch.

Step by Step Process:

Step 1: Install Angular CLI 8:

So, the very first thing that you need to do is to get to know how to get CLI up to speed. This step is very crucial since the official tool for initializing and running Angular applications is the Angular CLI. The first thing is writing the below mentioned line in your new terminal that you have just created:

nmp install – g @angular/cli

Step 2: Angular 8 Project Creation

Now to initialize your project use the following commands:

cd ~ ~

ng new angular-example

Post that CLI will be delivering a prompt to you, with a query of Angular routing addition. You have to accept the request. Once you do that there will be another prompt that will assist you to decide upon a stylesheet format that you will be using. Here you have to pick the CSS option.

Now, combining these two choices will enable Angular to create the necessary files and directories and establish the needed npm packages. But this is not it, as it will also set up the routing that we have stated previously automatically.

Now you can proceed to the root folder and run the local development server using the following commands:

cd angular-example

ng serve

These commands will make your application available at this address:

[http://localhost:4200/](http://localhost:4200/)

On this address using a web browsing you can check the status of your page, though it isn't still complete but still you can check the status.

Step 3- Addition of Angular HttpClient

While appearing to be command-heavy at first glance, this step is easy to complete. Thus, in order to complete this process of importing the HttpClientModule and adding it to the import array and assuring that the HttpClient will consume the REST API, we must go to the file src/app/app.module.ts and make the following changes:

import {BrowserModule} from ‘@angular/platform-browser’;

import {NgModule} from ‘@angular/core’;

important {AppRoutingModule} from’./app-routing.module’;

import {AppComponent} from’./app.component’;

import {HttpClientModule} from ‘@angular/common/http’;

@NgModule({

declaration: [

AppComponent,

],

import: [

BrowserModule,

AppRoutingModule,

HttpClientModule

],

providers: [],

bootstrap: [AppComponent]

})

Export class AppModule

Step 4- UI Component creation

As mentioned before, angular apps are made up of components. Now, to conclude that you have to open yet another new terminal and use the mentioned line of coding:

cd

~ ~

/angular-example

Ng g component home

Once you run these commands, it will displays the following output in our terminal:

CREATE src/app/home/home.component.html (19bytes)

CREATE src/app/home/home.component.spec.ts (614 bytes)

CREATE src/app/home/home.component.ts (261 bytes)

CREATE src/app/home/home.component.css (0 bytes)

UPDATE src/app/app.module.ts (467 bytes)

This leads us to the creation of “about” component by running the mentioned command:

<p style=”padding: 15px;”. This is the about page that describes your app</p>

Congratulations! You just have created a component.

Step 5- Routing Addition

Once again, this is a coding-heavy phase, but a critical one at that, since this step will construct the redirection of the empty path to the home component. The home page will be automatically redirected to first-time users in this precise sequence.

Step 6 Building UI with the help of Angular Material Component

You have to start by going to the root file of your project and adding the line of coding mentioned here:

ng add @angular/material

Following that, Angular will advise you to select a project theme. And because you are feeling experimental, let's go for the Indigo/Pink option. That will not be the end of it as the prompt will also ask us whether you would want to set up HammerJS for gesture recognition, as well as browser animations for Angular Material.

The modules for the following Material Design components are imported as a result.

1. MatToolbar: is used for headers, titles, or action containers;

2. MatCard - to provide text, images, and actions a place to go in the context of a single subject;

3. MatButton: it adds the style and ink ripples of Material Design to a native button> or a> element;

4. MatProgressSpinner - to offer a circular indicator of activity and progress.

Post the modules are imported, you can add these modules to the “import array” by running the code. Again, you have to open the file and update the code lines.

Step 7- Mocking Rest API

There are several small subsets that you must do after this one. But nothing to worry about; they are all easy to complete and won't take much time at all, but you will need to be very accurate in your movements.

This phase starts with switching to a fresh CLI and adding the JSON-server package from npm to the project. After that, you need to create a server folder in your Angular project’s root folder. You don’t have to leave here the server folder and still need to create a database.json file and then add a JSON object that will act as a database for the REST API server. The next thing is you need to go back to the command line and navigate back from the server folder to install Faker.js from npm. Now you have to create generate.js. Post that, you have to add the generate and server scripts we have into the package.json file. By going back to the command-line interface and running the generated script. Run your freshly baked REST API.

Now, these actions will allow sending HTTP request API endpoint.

Step 8- Consuming the best REST API with Angular HttpClient

Now you have to use HttpClient to have Angular use our REST API. The first step in this procedure is to build an Angular service that will include the code and enable us to access data from our REST API server.

Import and inject HttpClient, we then need to navigate to the src/app/api.service.ts file.

To send GET queries to our REST API endpoints, you have to now define the get() function. We now established a variable for the product and used the service's get() function to get data from the JSON REST API server. The next step is to change the src/app/home/home.component.html file.

Step 9- HTTP Error Handling

You have to update above mention part to the src/app/api.services.ts file. This will enable you to see the errors in our browser console.

Step 10- Pagination Addition

Using the Link header of the HTTP response that will be received from the REST API server, we add support for data pagination in this phase. As a result, we must ask HttpClient to provide you with the entire HttpResponse rather than just the response body. Use the option to "observe."

Start by importing the RxJS tap() operator into the src/app/data.service.ts file.

Step 11- Building and Development Angular Application to Firebase

Return to the CLI. To run the command, you must have access to the project's root folder.

You will get a prompt from the CLI to enter the authorization code. You will need to sign into the Google account connected to your Firebase account, where you may locate the authorization code, to paste it. The CLI will then ask you to choose a project when that is finished, which you have to do. The command-line interface will then update the angular.json file and construct the firebase.json and firebase files as necessary.

We are now prepared to launch the application on Firebase.

And last, your production has been added to Firebase hosting, which completes the development of our Angular App and leaves you with a good-to-go, usable new project in our possession.

This was a step-by-step guide to completing your Angular application. Hopefully, this was helpful for you to create an intuitive application for your project. In the near future and present Angular has established its importance in the app development field. You need to be hands-on with the development and for that, you can also hire the best web app development company and get a robust Angular application.

startup
Like

About the Creator

Grace Dobrev

I am a software developer currently working at Zenesys. I am here to share my knowledge related to software development, mobile app development, rpa services, etc.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.