File tree Expand file tree Collapse file tree
apps/signal/43-signal-input/src/app Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,8 +2,9 @@ import { TitleCasePipe } from '@angular/common';
22import {
33 ChangeDetectionStrategy ,
44 Component ,
5- Input ,
6- OnChanges ,
5+ computed ,
6+ input ,
7+ numberAttribute ,
78} from '@angular/core' ;
89
910type Category = 'Youth' | 'Junior' | 'Open' | 'Senior' ;
@@ -18,23 +19,20 @@ const ageToCategory = (age: number): Category => {
1819 selector : 'app-user' ,
1920 imports : [ TitleCasePipe ] ,
2021 template : `
21- {{ fullName | titlecase }} plays tennis in the {{ category }} category!!
22+ {{ fullName() | titlecase }} plays tennis in the {{ category() }} category!!
2223 ` ,
2324 host : {
2425 class : 'text-xl text-green-800' ,
2526 } ,
2627 changeDetection : ChangeDetectionStrategy . OnPush ,
2728} )
28- export class UserComponent implements OnChanges {
29- @ Input ( { required : true } ) name ! : string ;
30- @ Input ( ) lastName ?: string ;
31- @ Input ( ) age ?: string ;
29+ export class UserComponent {
30+ name = input . required < string > ( ) ;
31+ lastName = input < string > ( ) ;
32+ age = input < number , string > ( 0 , { transform : numberAttribute } ) ;
3233
33- fullName = '' ;
34- category : Category = 'Junior' ;
35-
36- ngOnChanges ( ) : void {
37- this . fullName = `${ this . name } ${ this . lastName ?? '' } ` ;
38- this . category = ageToCategory ( Number ( this . age ) ) ;
39- }
34+ fullName = computed ( ( ) => `${ this . name ( ) } ${ this . lastName ( ) ?? '' } ` ) ;
35+ category = computed ( ( ) => {
36+ return ageToCategory ( this . age ( ) ) ;
37+ } ) ;
4038}
You can’t perform that action at this time.
0 commit comments