From 2bc6210524272d3e16e42a45a314d6c5acbe4565 Mon Sep 17 00:00:00 2001 From: reedanj Date: Wed, 18 Mar 2026 20:08:14 +0000 Subject: [PATCH] feat: update user component to use input signal --- .../43-signal-input/src/app/user.component.ts | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/apps/signal/43-signal-input/src/app/user.component.ts b/apps/signal/43-signal-input/src/app/user.component.ts index 553042ebb..5fd3a3c66 100644 --- a/apps/signal/43-signal-input/src/app/user.component.ts +++ b/apps/signal/43-signal-input/src/app/user.component.ts @@ -2,8 +2,9 @@ import { TitleCasePipe } from '@angular/common'; import { ChangeDetectionStrategy, Component, - Input, - OnChanges, + computed, + input, + numberAttribute, } from '@angular/core'; type Category = 'Youth' | 'Junior' | 'Open' | 'Senior'; @@ -18,23 +19,18 @@ const ageToCategory = (age: number): Category => { selector: 'app-user', imports: [TitleCasePipe], template: ` - {{ fullName | titlecase }} plays tennis in the {{ category }} category!! + {{ fullName() | titlecase }} plays tennis in the {{ category() }} category!! `, host: { class: 'text-xl text-green-800', }, changeDetection: ChangeDetectionStrategy.OnPush, }) -export class UserComponent implements OnChanges { - @Input({ required: true }) name!: string; - @Input() lastName?: string; - @Input() age?: string; +export class UserComponent { + readonly name = input.required(); + readonly lastName = input(); + readonly age = input(0, { transform: numberAttribute }); - fullName = ''; - category: Category = 'Junior'; - - ngOnChanges(): void { - this.fullName = `${this.name} ${this.lastName ?? ''}`; - this.category = ageToCategory(Number(this.age)); - } + fullName = computed(() => `${this.name()} ${this.lastName() ?? ''}`); + category = computed(() => ageToCategory(this.age())); }