Angular Cheatsheet (2024) - Angular, A Popular Framework For - by Sehban Alam - Medium
Angular Cheatsheet (2024) - Angular, A Popular Framework For - by Sehban Alam - Medium
1. Project Setup
Install Angular CLI:
ng new my-angular-app
ng serve
Create a Component:
Decorators:
@NgModule : Declares an Angular module.
@Component : Declares a component.
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
bootstrap: [AppComponent]
})
export class AppModule { }
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<h1>{{ title }}</h1>`,
styles: ['h1 { color: blue; }']
})
export class AppComponent {
title = 'Hello Angular!';
}
3. Templates
Interpolation:
<p>{{ expression }}</p>
Property Binding:
<img [src]="imageUrl">
Event Binding:
Two-Way Binding:
<input [(ngModel)]="data">
4. Directives
Structural Directives:
*ngIf : Conditionally includes a template.
Attribute Directives:
ngClass : Dynamically adds/removes classes.
5. Data Binding
Types:
1. Interpolation: {{ data }}
@Injectable({
providedIn: 'root'
})
export class MyService {
getData() { return 'data'; }
}
@Component({
selector: 'app-root',
template: `<p>{{ data }}</p>`
})
export class AppComponent {
data: string;
constructor(private myService: MyService) {
this.data = this.myService.getData();
}
}
7. Routing
Configure Routes:
// app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
8. Forms
Template-Driven Form:
Reactive Form:
import { FormGroup, FormControl } from '@angular/forms';
9. HTTP Client
Setup:
Example:
// actions.ts
import { createAction } from '@ngrx/store';
// reducer.ts
import { createReducer, on } from '@ngrx/store';
import { increment } from './actions';
No responses yet
Feb 6 24 Jan 5 85 1
Lists