Angular Basics
Angular basics covers components and templates, data binding, directives, and pipes:
Components and Templates
Components are the fundamental building blocks of Angular applications. They consist of three main parts: a TypeScript class, an HTML template, and styles.
- Creating a Component: Use Angular CLI to generate a new component:
ng generate component hello
This creates a new directory with four files:
hello.component.ts
: The component classhello.component.html
: The HTML templatehello.component.css
: The component’s styleshello.component.spec.ts
: The test file
- Component Class:
import { Component } from '@angular/core';
@Component({
selector: 'app-hello',
templateUrl: './hello.component.html',
styleUrls: ['./hello.component.css']
})
export class HelloComponent {
name: string = 'World';
}
- Template:
<h1>Hello, {{ name }}!</h1>
Data Binding
Angular provides four forms of data binding:
- Interpolation: Displays component property values in the template.
<h1>{{ title }}</h1>
- Property Binding: Sets an element’s property to a component property value.
<img [src]="imageUrl">
- Event Binding: Listens for events and calls component methods.
<button (click)="onSave()">Save</button>
- Two-way Binding: Combines property and event binding, typically used with form inputs.
<input [(ngModel)]="name">
Note: To use ngModel
, you need to import FormsModule
in your Angular module.
Directives
Directives are classes that add behavior to elements in Angular applications. There are three kinds of directives:
-
Components: Directives with templates (which we’ve already covered).
-
Structural Directives: Change the DOM layout by adding and removing elements.
*ngIf
: Conditionally includes a template based on a boolean expression.
<div *ngIf="isLoggedIn">Welcome, User!</div>
*ngFor
: Repeats a template for each item in a list.
<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>
*ngSwitch
: Switches between alternative views.
<div [ngSwitch]="color">
<p *ngSwitchCase="'red'">The color is red</p>
<p *ngSwitchCase="'blue'">The color is blue</p>
<p *ngSwitchDefault>The color is neither red nor blue</p>
</div>
- Attribute Directives: Change the appearance or behavior of an element.
ngClass
: Adds or removes CSS classes.
<div [ngClass]="{'active': isActive, 'disabled': isDisabled}">Content</div>
ngStyle
: Adds or removes inline styles.
<div [ngStyle]="{'color': textColor, 'font-size': fontSize + 'px'}">Styled text</div>
Pipes
Pipes transform displayed values within a template. Angular provides several built-in pipes, and you can create custom pipes.
- Using pipes:
<p>{{ birthday | date }}</p>
<p>{{ price | currency:'USD' }}</p>
<p>{{ name | uppercase }}</p>
- Chaining pipes:
<p>{{ birthday | date:'fullDate' | uppercase }}</p>
- Pipe parameters: Some pipes accept parameters to customize their behavior.
<p>{{ birthday | date:'dd/MM/yyyy' }}</p>
- Common built-in pipes:
DatePipe
: Formats datesUpperCasePipe
: Transforms text to uppercaseLowerCasePipe
: Transforms text to lowercaseCurrencyPipe
: Formats numbers as currencyDecimalPipe
: Formats numbers with decimal pointsPercentPipe
: Formats numbers as percentages
- Custom Pipe: You can create custom pipes for specific formatting needs. Use Angular CLI to generate a pipe:
ng generate pipe exponential
Example implementation:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'exponential'
})
export class ExponentialPipe implements PipeTransform {
transform(value: number, exponent = 1): number {
return Math.pow(value, exponent);
}
}
Usage in template:
<p>{{ 2 | exponential:3 }}</p> <!-- Output: 8 -->
These Angular basics form the foundation of building Angular applications. As you become more comfortable with these concepts, you’ll be able to create more complex and interactive components and applications.
Remember to practice these concepts by building small components and gradually increasing complexity. The Angular documentation is an excellent resource for more detailed information on each of these topics.