-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathapp.component.ts
More file actions
41 lines (38 loc) · 960 Bytes
/
app.component.ts
File metadata and controls
41 lines (38 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Component, Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'wrapFunction',
pure: true,
})
export class WrapFunctionPipe implements PipeTransform {
transform<R>(fn: (...args: unknown[]) => R, ...args: unknown[]): R | null {
return fn(...args);
}
}
@Component({
selector: 'app-root',
imports: [WrapFunctionPipe],
template: `
@for (person of persons; track person.name) {
{{ showName | wrapFunction: person.name : $index }}
{{ isAllowed | wrapFunction: person.age : $first }}
}
`,
})
export class AppComponent {
persons = [
{ name: 'Toto', age: 10 },
{ name: 'Jack', age: 15 },
{ name: 'John', age: 30 },
];
showName(name: string, index: number) {
// very heavy computation
return `${name} - ${index}`;
}
isAllowed(age: number, isFirst: boolean) {
if (isFirst) {
return 'always allowed';
} else {
return age > 25 ? 'allowed' : 'declined';
}
}
}