Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions apps/signal/43-signal-input/src/app/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { TitleCasePipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
Input,
OnChanges,
computed,
input,
} from '@angular/core';

type Category = 'Youth' | 'Junior' | 'Open' | 'Senior';
Expand All @@ -19,23 +19,18 @@ 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<string>();
lastName = input<string | undefined>(undefined);
age = input<string | undefined>(undefined);

fullName = '';
category: Category = 'Junior';

ngOnChanges(): void {
this.fullName = `${this.name} ${this.lastName ?? ''}`;
this.category = ageToCategory(Number(this.age) ?? 0);
}
fullName = computed(() => `${this.name()} ${this.lastName() ?? ''}`);
category = computed(() => ageToCategory(Number(this.age()) ?? 0));
}
Loading