Angular
Angular
Example:
<nav>
<a routerLink="/home">Home</a>
<a routerLink="/about">About</a>
</nav>
<router-outlet></router-outlet>
2. Introduction to Angular
Angular is a TypeScript-based front-end framework.
Developed by Google for building SPAs.
Uses component-based architecture.
Features two-way data binding, dependency injection, and directives.
Supports modular development with reusable components.
Provides built-in HTTP Client for API integration.
Uses RxJS (Reactive Extensions) for handling asynchronous data.
Supports server-side rendering (SSR) via Angular Universal.
Works with Material Design for a modern UI.
Used by Google, PayPal, Upwork, and many enterprise apps.
Example:
Example:
ng new my-angular-app
cd my-angular-app
ng serve
Example:
@Component({
selector: 'app-root',
template: `<h1>Welcome to Angular!</h1>`
})
export class AppComponent {}
Example:
@Component({
selector: 'app-hello',
template: `<h2>Hello, Angular!</h2>`,
styles: [`h2 { color: blue; }`]
})
export class HelloComponent {}
6. Interpolation & Property Data Binding
Interpolation ( {{}} ) binds component variables to the view.
Example: {{ title }} renders the title variable dynamically.
Property Binding ( [property]="value" ) binds properties to DOM elements.
Example: <img [src]="imageUrl"> sets the image dynamically.
Property binding allows dynamic styling and content changes.
Ensures data flows from the component to the view (One-way binding).
Helps avoid manual DOM manipulation (like document.getElementById ).
Improves code readability and maintainability.
Can bind CSS styles, input values, and more.
Prevents XSS security vulnerabilities compared to innerHTML.
Example:
Example:
Example:
isError = true;
textColor = 'blue';
9. Directives
Directives are special attributes that modify the behavior of elements.
Angular has three types:
Structural Directives ( *ngIf , *ngFor ) change DOM structure.
Attribute Directives ( ngClass , ngStyle ) modify appearance.
Custom Directives extend Angular functionality.
*ngIf hides/shows elements dynamically.
*ngFor loops through lists in the template.
ngClass adds/removes CSS classes dynamically.
ngStyle applies inline styles dynamically.
Custom directives use the @Directive decorator.
Helps in building dynamic and interactive UIs.
Example:
Example:
Parent Component ( app.component.html )
Example:
<form #myForm="ngForm">
<input type="text" name="username" ngModel required>
<button [disabled]="!myForm.valid">Submit</button>
</form>
Example:
14. Observables
Observables handle asynchronous operations in Angular.
Used for HTTP calls, user events, and real-time data streams.
Part of RxJS (Reactive Extensions for JavaScript).
Works with subscribe() to listen for updates.
Helps in handling multiple async events smoothly.
Can be combined and manipulated using RxJS operators.
Better than Promises for handling multiple values over time.
Uses map() , filter() , debounceTime() , etc., for data transformation.
Improves performance and scalability of applications.
Works well with Angular’s HTTP Client for API calls.
Example:
this.http.get('https://api.example.com/data').subscribe(data => {
console.log(data);
});
Example:
Routing Configuration ( app-routing.module.ts )
<a routerLink="/home">Home</a>
<a routerLink="/about">About</a>
<router-outlet></router-outlet>