Angular basics
What is angular and what workflows is it ideal at?
Angular is a platform and framework for building single-page client applications using HTML and TypeScript. Developed and maintained by Google. t is designed to make the development and testing of such applications easier by providing a framework for client-side MVC (Model-View-Controller) architecture.
Installing Angular from scratch involves setting up your development environment, which includes installing Node.js, the Angular CLI (Command Line Interface), and creating a new Angular project. Here’s a step-by-step guide to get you started:
Step 1: Install Node.js
Download Node.js:
- Go to the Node.js official website.
Install Node.js:
- Run the installer and follow the prompts. Make sure to check the box that says “Automatically install the necessary tools” if prompted.
Verify Installation:
- Open a terminal (Command Prompt, PowerShell, or Terminal on macOS/Linux).
- Run the following commands to check if Node.js and npm are installed correctly:
- Restart the machine if the following prompts are not working.
Step 2: Install Angular CLI
The Angular CLI is a command-line tool that helps you create and manage Angular applications.
Install Angular CLI Globally:
- In your terminal, run the following command:
1
npm install -g @angular/cli
- The
-g
flag installs the Angular CLI globally, making it accessible from any directory.
- In your terminal, run the following command:
Verify Angular CLI Installation:
- After installation, check the version of Angular CLI by running:
1
ng version
- This command should display the version of Angular CLI along with other Angular-related packages.
- After installation, check the version of Angular CLI by running:
Creating an Angular project
Create a New Project:
- Navigate to the directory where you want to create your project. For example:
1
cd path/to/your/projects
- Run the following command to create a new Angular project (replace
my-angular-app
with your desired project name):or you can use the following to open a new project on the same folder1
ng new my-angular-app
1
ng new my-angular-app --directory=.
- You will be prompted to choose whether to add Angular routing and which stylesheet format to use (CSS, SCSS, SASS, LESS, etc.). Make your selections and press Enter. It is recommended to use SCSS for your first application
- Navigate to the directory where you want to create your project. For example:
Navigate to Your Project Directory:
- Change into your new project directory:
1
cd my-angular-app
- Change into your new project directory:
Running the application
- Serve the Application:
Creating a basic application
Creating components
Components are the building blocks of Angular applications. Each component encapsulates its own logic, template, and styles, making it reusable and maintainable.
- Creating a Component: To create a new component, you can use the Angular CLI. For example, to create a component named home:
|
|
This command will create a new folder named home in the src/app directory, containing the following files:
home.component.ts
: The TypeScript file containing the component’s logic.home.component.html
: The HTML template for the component.home.component.css
: The CSS styles for the component.home.component.spec.ts
: The testing file for the component.
Here is a breakdown of HomeComponents
- Contents of
HomeComponent.ts
- Contents of
home.component.html
Routing
Routing in Angular allows you to navigate between different views or components in your application without reloading the page. The Angular Router is responsible for this functionality.
Setting Up Routing
Import the RouterModule: In your app.routes.ts
, import RouterModule and define your routes.
|
|