Skip to content

Commit c1826be

Browse files
committed
feat(challenge 43): signal input my version
1 parent 8ce5402 commit c1826be

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

apps/signal/43-signal-input/src/app/user.component.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { TitleCasePipe } from '@angular/common';
22
import {
33
ChangeDetectionStrategy,
44
Component,
5-
Input,
6-
OnChanges,
5+
computed,
6+
input,
7+
numberAttribute,
8+
Signal,
79
} from '@angular/core';
810

911
type Category = 'Youth' | 'Junior' | 'Open' | 'Senior';
@@ -18,23 +20,21 @@ const ageToCategory = (age: number): Category => {
1820
selector: 'app-user',
1921
imports: [TitleCasePipe],
2022
template: `
21-
{{ fullName | titlecase }} plays tennis in the {{ category }} category!!
23+
{{ fullName() | titlecase }} plays tennis in the {{ category() }} category!!
2224
`,
2325
host: {
2426
class: 'text-xl text-green-800',
2527
},
2628
changeDetection: ChangeDetectionStrategy.OnPush,
2729
})
28-
export class UserComponent implements OnChanges {
29-
@Input({ required: true }) name!: string;
30-
@Input() lastName?: string;
31-
@Input() age?: string;
30+
export class UserComponent {
31+
name = input.required<string>();
32+
lastName = input<string>('');
33+
age = input<number, string>(0, { transform: numberAttribute });
3234

33-
fullName = '';
34-
category: Category = 'Junior';
35+
fullName: Signal<string> = computed(
36+
() => `${this.name()} ${this.lastName()}`,
37+
);
3538

36-
ngOnChanges(): void {
37-
this.fullName = `${this.name} ${this.lastName ?? ''}`;
38-
this.category = ageToCategory(Number(this.age) ?? 0);
39-
}
39+
category: Signal<Category> = computed(() => ageToCategory(this.age()));
4040
}

0 commit comments

Comments
 (0)