paint-brush
Getting Started with Angular: Build Robust Web Apps with Easeby@abdulwassaymajeed
366 reads
366 reads

Getting Started with Angular: Build Robust Web Apps with Ease

by Abdul Wassay MajeedJuly 6th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Master Angular - Build powerful web apps effortlessly. Learn installation, coding, and scalability. Harness reusability, modularity, and stunning UX. Your ultimate guide to unlocking Angular's potential.

People Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Getting Started with Angular: Build Robust Web Apps with Ease
Abdul Wassay Majeed HackerNoon profile picture

Introduction:

Angular has become a well-liked framework for creating dynamic and responsive web apps in the field of web development. Many developers turn to it as their first option because of its adaptability, performance, and powerful capabilities. We'll guide you through the steps of building an Angular web app from scratch in this blog article. This thorough manual will assist you in getting started with Angular and equip you to create robust online apps, regardless of your level of expertise with web development.

Installing Dependencies:

There are a few dependencies you must have in place before using Angular:


  • Understanding the principles of these web technologies will be helpful as Angular builds atop them. This includes HTML, CSS, and JavaScript.
  • NPM is the Node Package Manager. You can manage the dependencies and packages needed for your Angular project with the aid of these tools. Run the following command to ensure the installation of NPM:


npm -v
  • Built on Chrome's V8 engine, Node.js is a powerful JavaScript runtime that makes networking and server-side applications more effective and can be installed from its official website
  • Run the following command to ensure the installation of Node.js:
node -v

Install the LTS version, as it is supported for the long term.

Installing Angular CLI

To install the Angular CLI globally, launch a terminal or command prompt and enter the following command:

npm install -g @angular/cli

Creating a WebApp Using Angular

Use the Angular CLI to create a new project. Open your terminal and navigate to the directory where you want to create your project. After navigating to the directory, we have to run the following command:

ng new webapp

You can replace "webapp" with your preferred project name.

Angular CLI will prompt you to choose various project options, such as the routing feature and CSS preprocessor. Make your selections based on your project requirements.

Angular Routing:

Angular Routing is a mechanism provided by the Angular framework for building single-page applications (SPAs). It allows developers to define routes, or URL mappings, that correspond to different views or components in their application.

You should add Angular Routing to your projects.

Preprocessor:

In Angular, a preprocessor refers to a tool or technology that is used to transform and preprocess source code before it is compiled and executed by the browser.

You should choose SCSS as it provides enhanced features and improved code maintainability compared to regular CSS.

Figure 1: Command Window Prompt in setting Angular Webapp

Editing an Angular Project:

Now, after creating the webapp, we have to go to the project directory using the following command:

cd webapp

After going into the directory, run the following command to open the folder in VS Code:

code .

Elements and files involved in the Angular Project:

Following is an explanation of the components that are involved in the project:

src/appThis folder contains the core of your Angular application, including components, services, modules, and other related files.


src/assetsYou can store static assets such as images, icons, and stylesheets in this folder.


app/app.component.htmlThis is the main HTML file that serves as the entry point for your Angular app.


app/app.component.scssGlobal CSS styles for your application can be defined in this file.


app/app.component.tsThis is the entry point of your application, responsible for bootstrapping the Angular module.


app/app.module.tsIt defines the root module of your application and serves as the entry point for loading all other modules, components, services, and dependencies.


app/app-routing.module.tsIt defines the root module of your application and serves as the entry point for loading all other modules, components, services, and dependencies.

Generate a Component in Angular

Use the Angular CLI (Command Line Interface) to generate a new component:

ng g c mycomp

or

ng generate component mycomp

You can replace "mycomp" with the name of your component.

You will find several files related to the component. The key files are:


  • mycomp.component.tsThe TypeScript file where you define the component's logic

  • mycomp.component.htmlThe HTML template for the component's markup

  • mycomp.component.scssThe CSS file for styling the component


Edit these files according to your requirements.

Benefits of Components:

  1. Reusability: Code reuse
  2. Modularity: Smaller modules
  3. Encapsulation: Hidden implementation
  4. Maintainability: Easy maintenance
  5. Testability: Isolated testing
  6. Scalability: Easy expansion
  7. Collaboration: Improved teamwork

Generate a Service in Angular

Run the following Angular CLI command to generate a new service:

ng g s myserv

or

ng generate service myserv


Angular CLI will create the necessary files for your service in the project's directory structure. The service files will typically be generated in the "src/app" folder. Open the generated service file, .tsusually namedmyserv.service.ts, and define the logic and functionality of your service within the class.


To use the service in a component, you need to do the following:


  • Import the service into your component file.

    import { MyServiceService } from './myserv.service';
    
  • In the component's constructor, inject the service as a dependency.

    constructor(private myService: MyServiceService) { }
    

All these changes should be added to mycomp.component.ts

Benefits of a Service:

  1. Reusability: Code reuse
  2. Separation of Concerns: Clear responsibilities
  3. Dependency Injection: Loose Coupling
  4. Data Sharing: Centralized Data
  5. Single Responsibility Principle: Focused Functionality
  6. Testability: Isolated testing
  7. Scalability: Flexible growth
  8. Encapsulation: Hidden implementation

Building and Compiling an Angular Project

Now, make the changes according to your requirements and run the following command to build and compile your project:

ng serve

This command should be executed while you’re in the webapp directory

The command will build the project on “http://localhost:4200“. A preview is shown below:

Figure 2: Angular WebApp

Conclusion:

Creating a web app using Angular is an empowering journey. With a robust development environment, including Node.js and the Angular CLI, you can set up a project quickly.


Understanding the “app.module.ts” file helps organize imports, declarations, and providers, while “app.routing.ts” enables seamless navigation. Customization options allow for tailored components and services. Leveraging Angular's extensive ecosystem boosts productivity. With Angular's documentation and supportive community, developers can continually learn and refine their skills. Embrace Angular's power to build impressive, scalable web apps and enhance user experiences. Start your coding adventure and enjoy the process!

Credits:

Author: Abdul Wassay Majeed

Mentor: Muhammad Bilal https://hackernoon.com/u/th3n00bc0d3r