Skip to content

Commit 78db72a

Browse files
geromegrignond3lm
authored andcommitted
refactor: use signals (#127)
1 parent bbe2b05 commit 78db72a

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

content/components/minimize-logic-in-templates.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,42 @@ When we put too much logic in our templates, we are making our applications more
88

99
In addition, too much logic inside the template makes them less readable. We cannot take a quick glance at the template and quickly understand what's going on.
1010

11-
# Solution
12-
13-
Try to avoid putting too much logic in your templates.
14-
15-
For example here, we have have an `*ngIf` that has too much logic.
11+
For example here, we have have an `@if` that has too much logic.
1612

1713
```ts
1814
@Component({
19-
template: `<div *ngIf="users && users.length > 1 && visible">content to show</div>`,
15+
template: `
16+
@if(users().length && !users().some(user => user.criteriaMet)) {
17+
<p>No items meet the criteria.</p>
18+
}
19+
`,
2020
})
2121
export class SomeComponent {
22-
users: User[];
23-
visible: boolean;
22+
users: signal<User[]>([]);
2423
}
2524
```
2625

27-
We can refactor this by extracting the logic into the component's class. This will make the template more readable and the logic easier to test.
26+
# Solution
27+
28+
Let's refactor this by extracting the logic into the component's class. This will make the template more readable and the logic easier to test.
29+
To do so, we use `computed`, introduced in Angular 16, to create a computed property that will be used in the template.
2830

2931
```ts
3032
@Component({
31-
template: `<div *ngIf="usersExistsAndVisible()">content to show</div>`,
33+
template: `
34+
@if(noCriteriaMet()) {
35+
<p>No items meet the criteria.</p>
36+
}
37+
`,
3238
})
3339
export class SomeComponent {
34-
users: User[];
35-
visible: boolean;
36-
37-
usersExistsAndVisible() {
38-
return this.users && this.users.length > 1 && this.visible;
39-
}
40+
users: signal<User[]>([]);
41+
42+
noCriteriaMet = computed(() => this.users().length && !this.users().some(user => user.criteriaMet));
4043
}
4144
```
4245

4346
### Best Practices
4447

45-
Be careful when the `ChangeDetectionStrategy` is set to `Default`, as it'd cause the functions bound in the template to be called each time the `Change Detection Cycle` runs. You can optimize this by turning on the `OnPush` change detection strategy and leverage the `async` pipe in combination with `Observables` that return the desired value.
48+
Be careful when the `ChangeDetectionStrategy` is set to `Default`, as it'd cause the functions bound in the template to be called each time the `Change Detection Cycle` runs.
49+
You can optimize this by turning on the `OnPush` change detection strategy and leverage the `async` pipe in combination with `Observables` that return the desired value.

0 commit comments

Comments
 (0)