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.
There are a few dependencies you must have in place before using Angular:
npm -v
node -v
Install the LTS version, as it is supported for the long term.
To install the Angular CLI globally, launch a terminal or command prompt and enter the following command:
npm install -g @angular/cli
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 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.
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.
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 .
Following is an explanation of the components that are involved in the project:
src/app
This folder contains the core of your Angular application, including components, services, modules, and other related files.
src/assets
You can store static assets such as images, icons, and stylesheets in this folder.
app/app.component.html
This is the main HTML file that serves as the entry point for your Angular app.
app/app.component.scss
Global CSS styles for your application can be defined in this file.
app/app.component.ts
This is the entry point of your application, responsible for bootstrapping the Angular module.
app/app.module.ts
It 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.ts
It defines the root module of your application and serves as the entry point for loading all other modules, components, services, and dependencies.
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.ts
The TypeScript file where you define the component's logic
mycomp.component.html
The HTML template for the component's markup
mycomp.component.scss
The CSS file for styling the component
Edit these files according to your requirements.
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, .ts
usually 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
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:
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!
Author: Abdul Wassay Majeed
Mentor: Muhammad Bilal https://hackernoon.com/u/th3n00bc0d3r