diff --git a/apps/angular/signal-input/src/app/user.component.ts b/apps/angular/signal-input/src/app/user.component.ts index 082638bf6..245ea54d6 100644 --- a/apps/angular/signal-input/src/app/user.component.ts +++ b/apps/angular/signal-input/src/app/user.component.ts @@ -2,8 +2,9 @@ import { TitleCasePipe } from '@angular/common'; import { ChangeDetectionStrategy, Component, - Input, - OnChanges, + Signal, + computed, + input, } from '@angular/core'; type Category = 'Youth' | 'Junior' | 'Open' | 'Senior'; @@ -19,23 +20,24 @@ const ageToCategory = (age: number): Category => { standalone: true, 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 { + name = input.required(); + lastName = input(); + age = input(0, { + transform: (value: string): number => Number(value), + }); - fullName = ''; - category: Category = 'Junior'; - - ngOnChanges(): void { - this.fullName = `${this.name} ${this.lastName ?? ''}`; - this.category = ageToCategory(Number(this.age) ?? 0); - } + fullName: Signal = computed( + () => `${this.name()} ${this.lastName() ?? ''}`, + ); + category: Signal = computed(() => + ageToCategory(Number(this.age()) ?? 0), + ); }