Skip to content

Commit 51173a4

Browse files
feat(anchor navigation): resolved the challenge using routerlink and fragment
1 parent 805b53b commit 51173a4

4 files changed

Lines changed: 604 additions & 719 deletions

File tree

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { ApplicationConfig } from '@angular/core';
2-
import { provideRouter } from '@angular/router';
2+
import { provideRouter, withInMemoryScrolling } from '@angular/router';
33
import { appRoutes } from './app.routes';
44
export const appConfig: ApplicationConfig = {
5-
providers: [provideRouter(appRoutes)],
5+
providers: [
6+
provideRouter(
7+
appRoutes,
8+
withInMemoryScrolling({ anchorScrolling: 'enabled' }),
9+
),
10+
],
611
};

apps/angular/21-anchor-navigation/src/app/home.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { NavButtonComponent } from './nav-button.component';
88
<nav-button href="/foo" class="fixed left-1/2 top-3">Foo Page</nav-button>
99
<div id="top" class="h-screen bg-gray-500">
1010
Empty
11-
<nav-button href="#bottom">Scroll Bottom</nav-button>
11+
<nav-button fragment="bottom">Scroll Bottom</nav-button>
1212
</div>
1313
<div id="bottom" class="h-screen bg-blue-300">
1414
I want to scroll each
15-
<nav-button href="#top">Scroll Top</nav-button>
15+
<nav-button fragment="top">Scroll Top</nav-button>
1616
</div>
1717
`,
1818
})
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
/* eslint-disable @angular-eslint/component-selector */
22
import { Component, Input } from '@angular/core';
3+
import { RouterModule } from '@angular/router';
34
@Component({
45
selector: 'nav-button',
56
standalone: true,
67
template: `
7-
<a [href]="href">
8+
<a [routerLink]="href" [fragment]="fragment">
89
<ng-content></ng-content>
910
</a>
1011
`,
1112
host: {
1213
class: 'block w-fit border border-red-500 rounded-md p-4 m-2',
1314
},
15+
imports: [RouterModule],
1416
})
1517
export class NavButtonComponent {
1618
@Input() href = '';
19+
@Input() fragment: string | undefined = undefined;
1720
}

0 commit comments

Comments
 (0)