1. Home
  2. /
  3. Angular
  4. /
  5. Counter with Service

Counter with Service - Angular Typing CST Test

Loading…

Counter with Service — Angular Code

Demonstrates a counter component in Angular using a service to share state.

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

@Injectable({ providedIn: 'root' })
export class CounterService {
	count = 0;
	isDark = false;

	increment() { this.count++; }
	decrement() { this.count--; }
	reset() { this.count = 0; }
	toggleTheme() { this.isDark = !this.isDark; }
}

@Component({
	selector: 'app-counter-service',
	template: `
	<div [ngClass]="{ 'dark-theme': service.isDark, 'light-theme': !service.isDark }">
		<h2>Counter: {{ service.count }}</h2>
		<button (click)="service.increment()">+</button>
		<button (click)="service.decrement()">-</button>
		<button (click)="service.reset()">Reset</button>
		<button (click)="service.toggleTheme()">Toggle Theme</button>
	</div>
	`
})
export class CounterServiceComponent {
	constructor(public service: CounterService) {}
}

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.

More Angular Typing Exercises

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

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher