File tree Expand file tree Collapse file tree 1 file changed +12
-14
lines changed
apps/signal/43-signal-input/src/app Expand file tree Collapse file tree 1 file changed +12
-14
lines changed Original file line number Diff line number Diff line change @@ -2,8 +2,8 @@ import { TitleCasePipe } from '@angular/common';
22import {
33 ChangeDetectionStrategy ,
44 Component ,
5- Input ,
6- OnChanges ,
5+ computed ,
6+ input ,
77} from '@angular/core' ;
88
99type Category = 'Youth' | 'Junior' | 'Open' | 'Senior' ;
@@ -18,23 +18,21 @@ const ageToCategory = (age: number): Category => {
1818 selector : 'app-user' ,
1919 imports : [ TitleCasePipe ] ,
2020 template : `
21- {{ fullName | titlecase }} plays tennis in the {{ category }} category!!
21+ {{ fullName() | titlecase }} plays tennis in the {{ category() }} category!!
2222 ` ,
2323 host : {
2424 class : 'text-xl text-green-800' ,
2525 } ,
2626 changeDetection : ChangeDetectionStrategy . OnPush ,
2727} )
28- export class UserComponent implements OnChanges {
29- @Input ( { required : true } ) name ! : string ;
30- @Input ( ) lastName ?: string ;
31- @Input ( ) age ?: string ;
28+ export class UserComponent {
29+ name = input . required < string > ( ) ;
30+ lastName = input < string > ( ) ;
31+ age = input ( 0 , {
32+ transform : ( value : number | string ) =>
33+ typeof value === 'string' ? Number ( value ) : value ,
34+ } ) ;
3235
33- fullName = '' ;
34- category : Category = 'Junior' ;
35-
36- ngOnChanges ( ) : void {
37- this . fullName = `${ this . name } ${ this . lastName ?? '' } ` ;
38- this . category = ageToCategory ( Number ( this . age ) ?? 0 ) ;
39- }
36+ fullName = computed ( ( ) => `${ this . name ( ) } ${ this . lastName ( ) ?? '' } ` ) ;
37+ category = computed < Category > ( ( ) => ageToCategory ( this . age ( ) ?? 0 ) ) ;
4038}
You can’t perform that action at this time.
0 commit comments