- Angular is a TypeScript-based front-end framework developed by Google.
- Used to build Single Page Applications (SPA).
- Features: Two-way data binding, Dependency Injection (DI), Directives, Routing, RxJS (reactive programming).
-
Modules
- Root module:
AppModule - Feature modules: e.g.,
UserModule,AdminModule - Lazy-loaded modules for optimization.
- Root module:
-
Components
- Building blocks of UI.
- Consist of HTML template, CSS, and TypeScript class.
-
Templates
- Defines UI with HTML + Angular directives.
- Example:
*ngIf,*ngFor.
-
Directives
- Structural:
*ngIf,*ngFor,*ngSwitch. - Attribute:
ngClass,ngStyle, custom directives.
- Structural:
-
Services
- Used for business logic and data sharing.
- Provided via Dependency Injection (DI).
-
Dependency Injection
- Design pattern to inject service into components.
- Improves testability & reusability.
- Interpolation:
{{ data }} - Property Binding:
<img [src]="imageUrl"> - Event Binding:
<button (click)="handleClick()">Click</button> - Two-way Binding:
<input [(ngModel)]="username">
ngOnInit()→ Called after component initialization.ngOnChanges()→ Runs when input properties change.ngDoCheck()→ Developer custom change detection.ngAfterViewInit(),ngAfterContentInit()→ Called after view/content is initialized.ngOnDestroy()→ Cleanup (unsubscribe, remove listeners).
-
Used for SPA navigation.
-
AppRoutingModule defines routes:
const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'users/:id', component: UserComponent }, { path: '**', redirectTo: 'home' } ];
-
RouterLink:
<a routerLink="/home">Home</a> -
RouterOutlet:
<router-outlet></router-outlet>
-
Template-driven forms (simple, less scalable).
-
Reactive forms (preferred, scalable).
this.form = new FormGroup({ name: new FormControl('', Validators.required), email: new FormControl('', [Validators.required, Validators.email]) });
- Angular uses RxJS for async programming.
- Observable: Stream of data (HTTP response, events).
- Common operators:
map,filter,mergeMap,switchMap. - Must unsubscribe to avoid memory leaks (or use
asyncpipe).
-
Provided by
HttpClientModule. -
Example:
this.http.get<User[]>('/api/users') .subscribe(data => this.users = data);
- Local state with
BehaviorSubjectorService. - Global state with NgRx (Redux pattern).
✅ Use Reactive forms for complex forms. ✅ Always unsubscribe from observables. ✅ Use Lazy loading for feature modules. ✅ Follow OnPush change detection strategy for performance. ✅ Split large apps into feature modules.
- Signals: New reactive primitive to replace some RxJS use cases.
- Standalone Components: No need to declare in
NgModule. - Improved Hydration for SSR.
- Required Inputs: Inputs can be marked as required.