Skip to content

Commit 3d5aace

Browse files
committed
feat(challenge 21): angular-anchor-navigation my version
1 parent 0d15a0b commit 3d5aace

5 files changed

Lines changed: 26 additions & 10 deletions

File tree

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
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({
9+
anchorScrolling: 'enabled',
10+
scrollPositionRestoration: 'enabled',
11+
}),
12+
),
13+
],
614
};

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { NavButtonComponent } from './nav-button.component';
66
selector: 'app-foo',
77
template: `
88
Welcome to foo page
9-
<nav-button href="home" class="fixed left-1/2 top-3">Home Page</nav-button>
9+
<nav-button [href]="'/home'" class="fixed left-1/2 top-3">
10+
Home Page
11+
</nav-button>
1012
<div class="h-screen bg-blue-200">section 1</div>
1113
<div class="h-screen bg-red-200">section 2</div>
1214
`,

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ import { Component } from '@angular/core';
22
import { NavButtonComponent } from './nav-button.component';
33

44
@Component({
5-
imports: [NavButtonComponent],
65
selector: 'app-home',
6+
imports: [NavButtonComponent],
77
template: `
8-
<nav-button href="/foo" class="fixed left-1/2 top-3">Foo Page</nav-button>
8+
<nav-button [href]="'/foo'" class="fixed left-1/2 top-3">
9+
Foo Page
10+
</nav-button>
911
<div id="top" class="h-screen bg-gray-500">
1012
Empty
11-
<nav-button href="#bottom">Scroll Bottom</nav-button>
13+
<nav-button [href]="'/home'" [fragment]="'bottom'">
14+
Scroll Bottom
15+
</nav-button>
1216
</div>
1317
<div id="bottom" class="h-screen bg-blue-300">
1418
I want to scroll each
15-
<nav-button href="#top">Scroll Top</nav-button>
19+
<nav-button [href]="'/home'" [fragment]="'top'">Scroll Top</nav-button>
1620
</div>
1721
`,
1822
})
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/* eslint-disable @angular-eslint/component-selector */
22
import { Component, Input } from '@angular/core';
3+
import { RouterLinkWithHref } from '@angular/router';
34
@Component({
45
selector: 'nav-button',
5-
standalone: true,
6+
imports: [RouterLinkWithHref],
67
template: `
7-
<a [href]="href">
8+
<a [routerLink]="href" [fragment]="fragment">
89
<ng-content></ng-content>
910
</a>
1011
`,
@@ -14,4 +15,5 @@ import { Component, Input } from '@angular/core';
1415
})
1516
export class NavButtonComponent {
1617
@Input() href = '';
18+
@Input() fragment: string | undefined = undefined;
1719
}

apps/angular/21-anchor-navigation/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html>
2-
<html lang="en">
2+
<html lang="en" class="scroll-smooth">
33
<head>
44
<meta charset="utf-8" />
55
<title>angular-anchor-navigation</title>

0 commit comments

Comments
 (0)