Angular 15 basic tutorial with examples
Angular 15 Basic Tutorial with Examples
Angular 15 is the latest version of the Angular framework that enables developers to build powerful, scalable, and maintainable web applications. In this tutorial, we will walk through the basic concepts of Angular 15, such as setting up an Angular application, creating components, working with templates, data binding, directives, services, and routing.
1. Setting Up an Angular Project
To get started with Angular 15, you need to install Node.js and Angular CLI. Follow the steps below:
Install Node.js and npm
Download and install Node.js from nodejs.org. This will also install npm (Node Package Manager), which is required to manage your project dependencies.
Install Angular CLI
The Angular CLI (Command Line Interface) is a tool to easily create Angular projects and run them in development mode.
To install Angular CLI globally, open your terminal or command prompt and run:
Create a New Angular Application
Once the Angular CLI is installed, you can create a new Angular project using the following command:
You will be prompted to choose options for your project, such as whether to add Angular routing or choose a stylesheet format (CSS, SCSS, etc.). You can choose the default settings for now.
After the project is created, navigate to the project directory:
Serve the Application
To start the development server and see your application in action, run:
This will start the app, and you can view it at http://localhost:4200 in your browser.
2. Angular Components
Components are the building blocks of Angular applications. Each component consists of:
- HTML for the template.
- CSS for styling.
- TypeScript for logic.
Creating a Component
To generate a new component, run:
This creates four files:
hello-world.component.ts(the TypeScript file for component logic)hello-world.component.html(the template for the component)hello-world.component.css(the style file for the component)hello-world.component.spec.ts(for unit tests)
Basic Component Example
In hello-world.component.ts:
In hello-world.component.html:
In hello-world.component.css:
Now, use the app-hello-world component in the app.component.html (the root component) by replacing the content with:
This will render "Hello, Angular 15!" in blue color when you run the application.
3. Data Binding in Angular
Angular provides several ways to bind data between components and templates.
1. Interpolation (Text Binding)
Used for binding component data to the template:
2. Property Binding
Bind a property of an HTML element to a component property:
In the component:
3. Event Binding
Bind events (e.g., button clicks) to methods in the component:
In the component:
4. Two-Way Binding
Angular provides ngModel for two-way data binding. For this, you need to import the FormsModule in the app.module.ts.
First, import FormsModule:
Then, in your component:
This will create an input box that updates the message property in real-time as the user types.
4. Directives in Angular
Directives are used to manipulate the DOM or bind data to DOM elements.
1. Structural Directives (e.g., *ngIf, *ngFor)
*ngIf: Conditionally include an element in the DOM.
In the component:
*ngFor: Loop through a list and display each item.
In the component:
2. Attribute Directives (e.g., ngClass, ngStyle)
ngClass: Conditionally apply classes to elements.
In the component:
ngStyle: Apply inline styles dynamically.
In the component:
5. Services and Dependency Injection
Services in Angular are used to share data and logic across components. You can inject services into components using Angular's Dependency Injection (DI) system.
1. Creating a Service
To generate a service, use Angular CLI:
In the generated message.service.ts:
2. Injecting a Service into a Component
In the component, you can inject the service using the constructor:
In the template:
6. Routing in Angular
Routing allows you to navigate between different views or pages in an Angular application.
1. Configuring Routes
In the app-routing.module.ts file, configure the routes:
2. Adding RouterLink for Navigation
In the template, use routerLink to create links for navigation:
The <router-outlet></router-outlet> is where the routed component will be displayed.
Conclusion
In this tutorial, we have covered the basics of Angular 15, including:
- Setting up a new Angular project.
- Creating and using components.
- Working with data binding (interpolation, property binding, event binding, and two-way binding).
- Using directives like
*ngIf,*ngFor,ngClass, andngStyle. - Creating services and using dependency injection.
- Setting up routing to navigate between different views.
Comments
Post a Comment