Counter (Standalone Component)
signal, update and computedRequirements
Use a standalone Angular component as the root component. Display the current count value. Provide "+", "-", and "Reset" buttons. Disable the decrement button when the count is 0.
What the checks look for
The checks find elements by .value, button "-", button "+", button "Reset". Keep these in your markup.
Expected behavior
Clicking "+" increases the count by 1. Clicking "-" decreases the count by 1, but never below 0. Clicking "Reset" sets the count back to 0.
Implementation notes
Hold the count in a signal ( count = signal(0)) and change it withupdateorset; the template reads it ascount(). A plaincount = 0field also works here, because every change comes from a click in this template.Use template event bindings like (click)="..."for button actions.Derive the disabled state with computedinstead of repeatingcount() === 0in the template. No child components are needed.
Solution idea
The counter needs one piece of state, the current count. The solution keeps it in a signal, count = signal(0), on the standalone root component and derives isZero from it with computed. The three buttons call inc(), dec() and reset(), which change the signal with update and set. The template reads count() and binds [disabled]="isZero()" on the minus button, so Angular knows which bindings depend on the count and the component can use OnPush change detection.
Solution
Overview
The counter needs one piece of state, the current count. The solution keeps it in a signal, count = signal(0), on the standalone root component and derives isZero from it with computed. The three buttons call inc(), dec() and reset(), which change the signal with update and set. The template reads count() and binds [disabled]="isZero()" on the minus button, so Angular knows which bindings depend on the count and the component can use OnPush change detection.
Test cases
- Click "+" once → count becomes
1. - Click "+" multiple times → count increases by 1 on each click.
- With count at
0, "-" is disabled and clicking it does nothing. - From a positive value, clicking "-" decreases the count by 1, but never below
0. - From a positive value, clicking "Reset" sets count back to
0and disables "-" again.
Signal state with a computed disabled flag
Keep the whole counter in AppComponent.
- Declare
count = signal(0). Readingcount()in the template ties that binding to the signal. - Derive
isZero = computed(() => this.count() === 0). It recalculates only whencountchanges, and the template binds it to the minus button'sdisabled. inc()callscount.update((value) => value + 1)andreset()callscount.set(0).dec()clamps withMath.max(0, value - 1), so the count cannot drop below zero even if something callsdec()while the button is disabled.- In the template, the interpolation becomes
{{ count() }}and the minus button binds[disabled]="isZero()".
ChangeDetectionStrategy.OnPush is safe because every value the template shows comes from a signal. A plain field with an isZero getter would also pass here, since every change starts with a click inside this template. The signal version keeps working when the count later changes from a timer, a service or a parent, where a plain field under OnPush or zoneless change detection leaves the view stale.
Files
{
"name": "angular-counter-solution",
"private": true
}
{
"compilerOptions": {
"target": "es2022",
"strict": true,
"noEmitOnError": false
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Angular SDK • Counter (solution)</title>
</head>
<body>
<app-root></app-root>
</body>
</html>
import 'zone.js';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent).catch(console.error);
:root {
color-scheme: dark;
}
html, body {
margin: 0;
height: 100%;
background: #0a0a0a;
}
body {
font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}
.panel {
background: #0b1020;
border: 1px solid #1f2937;
border-radius: 12px;
padding: 16px;
}
.value {
font-size: 48px;
line-height: 1;
font-weight: 800;
text-align: center;
margin-bottom: 12px;
}
.actions {
display: flex;
gap: 8px;
justify-content: center;
}
button {
background: #a3e635;
color: #111;
border: none;
border-radius: 10px;
padding: 10px 14px;
font-weight: 700;
cursor: pointer;
}
button[disabled] {
opacity: .5;
cursor: not-allowed;
}
button.ghost {
background: transparent;
color: #e5e7eb;
border: 1px solid #374151;
}
<main style="min-height:100vh; background:#0a0a0a; color:#e5e7eb; display:grid; place-items:center; padding:24px;">
<section style="width:100%; max-width:420px; display:grid; gap:16px;">
<h1 style="margin:0; font-size:20px; font-weight:700;">Counter (Solution)</h1>
<div class="panel">
<div class="value">{{ count() }}</div>
<div class="actions">
<button (click)="dec()" [disabled]="isZero()" aria-label="decrement">−</button>
<button (click)="inc()" aria-label="increment">+</button>
<button (click)="reset()" class="ghost" aria-label="reset">Reset</button>
</div>
</div>
</section>
</main>
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-root',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
readonly count = signal(0);
readonly isZero = computed(() => this.count() === 0);
inc(): void {
this.count.update((value) => value + 1);
}
dec(): void {
// The disabled button stops clicks at 0; the clamp keeps the rule true for any other caller.
this.count.update((value) => Math.max(0, value - 1));
}
reset(): void {
this.count.set(0);
}
}
Notes & Pitfalls
Allowing the count to go below 0 when handling the decrement. Forgetting to bind the disabled state of the decrement button, which makes the UX unclear. Writing {{ count }}instead of{{ count() }}: the template then shows the signal function, not the number.Changing the count inline in the template ( count.set(count() - 1)) instead of in methods, which spreads the zero guard across bindings.
Standalone components are the default since Angular 19, so the decorator needs neither standalone: truenorCommonModule.Holding state in a signaland changing it withsetandupdate.Deriving view state with computedinstead of a getter.Using OnPushchange detection, which signal-driven templates keep correct.
Common mistakes on this challenge
Fix the Angular template compilation contract first
Framework assertions cannot be trusted until the standalone component and its modern control-flow template compile and boot. Fix the first Angular compiler diagnostic, then check every @if,@for, or@switchblock for balanced braces and a validtrackexpression.Verify standalone importsincludes the forms modules, directives, or pipes the template actually uses; built-in control flow alone does not requireCommonModule.Wait for the preview to boot successfully before debugging assertion mismatches, then re-run one behavior check at a time.
Call a signal where the template reads it
The page shows a "[Signal: …]" label, which Angular prints when a template reads a signal without calling it. Write count()rather thancountin interpolations: a signal is a function, and the template needs its value.Check property bindings as well: [disabled]="isEmpty"binds the function, which is always truthy, while[disabled]="isEmpty()"binds the boolean.Inputs, models and computed values read the same way, for example rating(),value()orfilteredUsers()inside@for.
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.