Skip to content

Answer:21 #1317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apps/angular/21-anchor-navigation/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideRouter, withInMemoryScrolling } from '@angular/router';
import { appRoutes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(appRoutes)],
providers: [
provideRouter(
appRoutes,
withInMemoryScrolling({
anchorScrolling: 'enabled',
scrollPositionRestoration: 'enabled',
}),
),
],
};
4 changes: 3 additions & 1 deletion apps/angular/21-anchor-navigation/src/app/foo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { NavButtonComponent } from './nav-button.component';
selector: 'app-foo',
template: `
Welcome to foo page
<nav-button href="home" class="fixed left-1/2 top-3">Home Page</nav-button>
<nav-button [href]="'/home'" class="fixed left-1/2 top-3">
Home Page
</nav-button>
<div class="h-screen bg-blue-200">section 1</div>
<div class="h-screen bg-red-200">section 2</div>
`,
Expand Down
12 changes: 8 additions & 4 deletions apps/angular/21-anchor-navigation/src/app/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import { Component } from '@angular/core';
import { NavButtonComponent } from './nav-button.component';

@Component({
imports: [NavButtonComponent],
selector: 'app-home',
imports: [NavButtonComponent],
template: `
<nav-button href="/foo" class="fixed left-1/2 top-3">Foo Page</nav-button>
<nav-button [href]="'/foo'" class="fixed left-1/2 top-3">
Foo Page
</nav-button>
<div id="top" class="h-screen bg-gray-500">
Empty
<nav-button href="#bottom">Scroll Bottom</nav-button>
<nav-button [href]="'/home'" [fragment]="'bottom'">
Scroll Bottom
</nav-button>
</div>
<div id="bottom" class="h-screen bg-blue-300">
I want to scroll each
<nav-button href="#top">Scroll Top</nav-button>
<nav-button [href]="'/home'" [fragment]="'top'">Scroll Top</nav-button>
</div>
`,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-disable @angular-eslint/component-selector */
import { Component, Input } from '@angular/core';
import { RouterLinkWithHref } from '@angular/router';
@Component({
selector: 'nav-button',
standalone: true,
imports: [RouterLinkWithHref],
template: `
<a [href]="href">
<a [routerLink]="href" [fragment]="fragment">
<ng-content></ng-content>
</a>
`,
Expand All @@ -14,4 +15,5 @@ import { Component, Input } from '@angular/core';
})
export class NavButtonComponent {
@Input() href = '';
@Input() fragment: string | undefined = undefined;
}
2 changes: 1 addition & 1 deletion apps/angular/21-anchor-navigation/src/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en">
<html lang="en" class="scroll-smooth">
<head>
<meta charset="utf-8" />
<title>angular-anchor-navigation</title>
Expand Down