1. Home
  2. /
  3. Angular
  4. /
  5. Counter with Lifecycle Hooks

Counter with Lifecycle Hooks - Angular Typing CST Test

Loading…

Counter with Lifecycle Hooks — Angular Code

Shows use of Angular lifecycle hooks ngOnInit and ngOnDestroy in a counter component.

import { Component, OnInit, OnDestroy } from '@angular/core';

@Component({
	selector: 'app-lifecycle-counter',
	template: `
	<div>
		<h2>Counter: {{ count }}</h2>
		<button (click)="increment()">+</button>
		<button (click)="decrement()">-</button>
	</div>
	`
})
export class LifecycleCounterComponent implements OnInit, OnDestroy {
	count = 0;
	ngOnInit() { console.log('Counter initialized'); }
	ngOnDestroy() { console.log('Counter destroyed'); }
	increment() { this.count++; }
	decrement() { this.count--; }
}

Angular Language Guide

Angular is a platform and framework for building client-side web applications using HTML, CSS, and TypeScript. Developed by Google, it is designed for building single-page applications (SPAs) with rich user interfaces and reactive features.

Primary Use Cases

  • ▸Single-page applications (SPAs)
  • ▸Enterprise web apps with complex UIs
  • ▸Progressive Web Apps (PWAs)
  • ▸Dynamic dashboards and admin panels
  • ▸Cross-platform web applications with Angular Universal

Notable Features

  • ▸Component-based architecture
  • ▸Two-way data binding
  • ▸Dependency injection
  • ▸TypeScript support and static typing
  • ▸Reactive programming with RxJS

Origin & Creator

Originally created in 2010 as AngularJS by Misko Hevery and maintained by Google; rewritten as Angular (2+) in 2016.

Industrial Note

Angular is widely used in enterprise-grade web applications, dashboards, and SPAs where maintainability, tooling, and structured architecture are important.

Quick Explain

  • ▸Angular allows developers to build modular, scalable, and maintainable web applications.
  • ▸It uses TypeScript for strong typing and modern JavaScript features.
  • ▸Angular employs a component-based architecture for reusable UI and logic.

Core Features

  • ▸Modules for code organization
  • ▸Services for business logic
  • ▸Directives for dynamic DOM manipulation
  • ▸Routing for SPA navigation
  • ▸Reactive forms and template-driven forms

Learning Path

  • ▸Learn TypeScript basics
  • ▸Understand Angular components and templates
  • ▸Practice data binding and directives
  • ▸Learn services, dependency injection, and RxJS
  • ▸Build full-featured SPA and test it

Practical Examples

  • ▸Dynamic dashboard with charts and tables
  • ▸E-commerce web application
  • ▸Real-time chat app using WebSockets
  • ▸PWA with offline capabilities
  • ▸Admin panel with forms and CRUD operations

Comparisons

  • ▸More structured than React or Vue for large apps
  • ▸Stronger typing with TypeScript than plain JS frameworks
  • ▸Enterprise-friendly with CLI and tooling
  • ▸Larger bundle size than lightweight frameworks
  • ▸Supports reactive programming better than jQuery

Strengths

  • ▸Strong typing and early error detection with TypeScript
  • ▸Powerful CLI and developer tooling
  • ▸Structured and maintainable architecture
  • ▸Large ecosystem and community support
  • ▸Optimized for complex, enterprise-grade applications

Limitations

  • ▸Steep learning curve for beginners
  • ▸Verbosity compared to lightweight frameworks
  • ▸Requires understanding of TypeScript and RxJS
  • ▸Heavier bundle size than simpler frameworks
  • ▸Overkill for very small or simple apps

When NOT to Use

  • ▸Simple static websites or blogs
  • ▸Small projects where Vue or React is sufficient
  • ▸Projects where learning TypeScript adds overhead
  • ▸Apps requiring very lightweight bundles
  • ▸Rapid prototyping for small features

Cheat Sheet

  • ▸ng new project-name - create new project
  • ▸ng serve - run app locally
  • ▸ng generate component my-component - create component
  • ▸[(ngModel)] - two-way data binding
  • ▸*ngIf / *ngFor - structural directives

FAQ

  • ▸Is Angular a framework or library?
  • ▸Angular is a full-fledged framework.
  • ▸Can Angular work with TypeScript?
  • ▸Yes, TypeScript is the primary language for Angular.
  • ▸Does Angular support SPAs?
  • ▸Yes, Angular is optimized for single-page applications.
  • ▸What is Angular CLI?
  • ▸Command-line tool to create and manage Angular projects.
  • ▸Is Angular good for enterprise apps?
  • ▸Yes, it's widely used in enterprise-grade web applications.

30-Day Skill Plan

  • ▸Week 1: Angular CLI, TypeScript, and components
  • ▸Week 2: Modules, services, and routing
  • ▸Week 3: Forms, pipes, and directives
  • ▸Week 4: RxJS, observables, and state management
  • ▸Week 5: Testing, deployment, and optimization

Final Summary

  • ▸Angular is a structured framework for building scalable SPAs.
  • ▸Uses TypeScript, components, and reactive programming.
  • ▸Supports enterprise-level development with strong tooling.
  • ▸Optimized for maintainable, modular, and testable apps.
  • ▸Well-suited for complex UI and cross-platform web applications.

Project Structure

  • ▸src/app/ - main application code
  • ▸src/assets/ - static files
  • ▸src/environments/ - environment configuration
  • ▸angular.json - build configuration
  • ▸package.json - dependencies and scripts

Monetization

  • ▸Enterprise SaaS web applications
  • ▸Progressive web apps
  • ▸Admin dashboards for clients
  • ▸E-commerce frontends
  • ▸Educational courses in Angular

Productivity Tips

  • ▸Use Angular CLI for scaffolding
  • ▸Follow component modularity best practices
  • ▸Leverage RxJS for reactive workflows
  • ▸Use Angular Material for UI consistency
  • ▸Optimize bundle sizes with lazy loading

Basic Concepts

  • ▸Components and templates
  • ▸Modules and NgModule
  • ▸Services and dependency injection
  • ▸Data binding: interpolation, property, event, two-way
  • ▸Directives: structural and attribute

Official Docs

  • ▸https://angular.io/docs
  • ▸https://angular.io/guide/architecture
  • ▸https://angular.io/cli

More Angular Typing Exercises

Angular Counter ComponentAngular Counter with ServiceAngular Counter with Two-Way BindingAngular Counter with EventEmitterAngular Reactive CounterAngular Counter with LocalStorageAngular Counter with AsyncPipeAngular Counter with Input PropertyAngular Counter with Output Event

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher