Skip to content

Commit b382acc

Browse files
tomalaforgeslimabdi
authored andcommitted
fix: resolve change detection loop
1 parent 543770b commit b382acc

1 file changed

Lines changed: 7 additions & 13 deletions

File tree

apps/angular/32-change-detection-bug/src/app/main-navigation.component.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, inject, input } from '@angular/core';
1+
import { Component, computed, inject, input } from '@angular/core';
22
import { toSignal } from '@angular/core/rxjs-interop';
33
import { RouterLink, RouterLinkActive } from '@angular/router';
44
import { FakeServiceService } from './fake.service';
@@ -38,25 +38,19 @@ export class NavigationComponent {
3838
@Component({
3939
imports: [NavigationComponent],
4040
template: `
41-
@if (info() !== null) {
42-
<app-nav [menus]="getMenu(info()!)" />
43-
} @else {
44-
<app-nav [menus]="getMenu('')" />
45-
}
41+
<app-nav [menus]="menus()" />
4642
`,
4743
host: {},
4844
})
4945
export class MainNavigationComponent {
5046
private fakeBackend = inject(FakeServiceService);
5147

5248
readonly info = toSignal(this.fakeBackend.getInfoFromBackend(), {
53-
initialValue: null,
49+
initialValue: '',
5450
});
5551

56-
getMenu(prop: string) {
57-
return [
58-
{ path: '/foo', name: `Foo ${prop}` },
59-
{ path: '/bar', name: `Bar ${prop}` },
60-
];
61-
}
52+
readonly menus = computed<MenuItem[]>(() => [
53+
{ path: '/foo', name: `Foo ${this.info()}` },
54+
{ path: '/bar', name: `Bar ${this.info()}` },
55+
]);
6256
}

0 commit comments

Comments
 (0)