File tree Expand file tree Collapse file tree 3 files changed +5305
-5722
lines changed
apps/signal/43-signal-input/src/app Expand file tree Collapse file tree 3 files changed +5305
-5722
lines changed Original file line number Diff line number Diff line change @@ -2,8 +2,8 @@ import { TitleCasePipe } from '@angular/common';
2
2
import {
3
3
ChangeDetectionStrategy ,
4
4
Component ,
5
- Input ,
6
- OnChanges ,
5
+ computed ,
6
+ input ,
7
7
} from '@angular/core' ;
8
8
9
9
type Category = 'Youth' | 'Junior' | 'Open' | 'Senior' ;
@@ -14,28 +14,28 @@ const ageToCategory = (age: number): Category => {
14
14
return 'Senior' ;
15
15
} ;
16
16
17
+ function parseAge ( age : string | undefined ) : number {
18
+ if ( ! age ) return 0 ;
19
+ return + age ;
20
+ }
21
+
17
22
@Component ( {
18
23
selector : 'app-user' ,
19
24
standalone : true ,
20
25
imports : [ TitleCasePipe ] ,
21
26
template : `
22
- {{ fullName | titlecase }} plays tennis in the {{ category }} category!!
27
+ {{ fullName() | titlecase }} plays tennis in the {{ category() }} category!!
23
28
` ,
24
29
host : {
25
30
class : 'text-xl text-green-800' ,
26
31
} ,
27
32
changeDetection : ChangeDetectionStrategy . OnPush ,
28
33
} )
29
- export class UserComponent implements OnChanges {
30
- @Input ( { required : true } ) name ! : string ;
31
- @Input ( ) lastName ?: string ;
32
- @Input ( ) age ?: string ;
33
-
34
- fullName = '' ;
35
- category : Category = 'Junior' ;
34
+ export class UserComponent {
35
+ name = input . required < string > ( ) ;
36
+ lastName = input < string > ( ) ;
37
+ age = input ( 0 , { transform : parseAge } ) ;
36
38
37
- ngOnChanges ( ) : void {
38
- this . fullName = `${ this . name } ${ this . lastName ?? '' } ` ;
39
- this . category = ageToCategory ( Number ( this . age ) ?? 0 ) ;
40
- }
39
+ fullName = computed ( ( ) => `${ this . name ( ) } ${ this . lastName ( ) ?? '' } ` ) ;
40
+ category = computed ( ( ) => ageToCategory ( this . age ( ) ?? 0 ) ) ;
41
41
}
You can’t perform that action at this time.
0 commit comments