Skip to content

Commit c24ddea

Browse files
Ismaestroiramos
authored andcommitted
feat(anchor): add anchor and scroll support in app routing module
1 parent f800efe commit c24ddea

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ npm run update | Update the project dependencies with ng update
7171
* Modal and toasts (snakbar)!
7272
* Responsive layout (flex layout module)
7373
* SASS (most common used functions and mixins) and BEM styles
74-
* Internationalization with ng-translate and ngx-translate-extract
74+
* Internationalization with ng-translate and ngx-translate-extract. Also use cache busting for translation files with [webpack translate loader](https://github.com/ngx-translate/http-loader#angular-cliwebpack-translateloader-example)
7575
* Lazy loading modules
7676
* Service Workers
7777
* Dynamic Imports
7878
* More logical structure directory (from [here](https://itnext.io/choosing-a-highly-scalable-folder-structure-in-angular-d987de65ec7))
7979
* Basic example library
80+
* Scroll restoration and anchor examples
8081
* Modernizr (browser features detection)
8182
* Safari polyfill for date inputs (date-input-polyfill)
8283
* Google Tag Manager

projects/ngx-example-library/karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ module.exports = function (config) {
2626
logLevel: config.LOG_INFO,
2727
autoWatch: true,
2828
browsers: ['Chrome'],
29-
singleRun: false
29+
singleRun: true
3030
});
3131
};

src/app/app-routing.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ const routes: Routes = [
1616

1717
@NgModule({
1818
imports: [
19-
RouterModule.forRoot(routes, {scrollPositionRestoration: 'enabled'})
19+
RouterModule.forRoot(routes, {
20+
scrollPositionRestoration: 'enabled',
21+
anchorScrolling: 'enabled'
22+
})
2023
],
2124
exports: [
2225
RouterModule

src/app/modules/heroes/pages/hero-detail/hero-detail.page.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ <h1 class="header__title">{{'heroDetail' | translate}}</h1>
3535
<button mat-raised-button type="button" (click)="goBack()">
3636
{{'goBack' | translate | uppercase}}
3737
</button>
38+
<button *ngIf="hero" mat-raised-button type="button" (click)="goToTheAnchor()">
39+
{{'Anchor to this hero' | translate | uppercase}}
40+
</button>

src/app/modules/heroes/pages/hero-detail/hero-detail.page.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {Component, OnInit} from '@angular/core';
22
import {Hero} from '../../shared/hero.model';
33
import {HeroService} from '../../shared/hero.service';
4-
import {ActivatedRoute} from '@angular/router';
4+
import {ActivatedRoute, Router} from '@angular/router';
55
import {Location} from '@angular/common';
6+
import {AppConfig} from '../../../../config/app.config';
67

78
@Component({
89
selector: 'app-hero-detail',
@@ -17,6 +18,7 @@ export class HeroDetailPage implements OnInit {
1718

1819
constructor(private heroService: HeroService,
1920
private location: Location,
21+
private router: Router,
2022
private activatedRoute: ActivatedRoute) {
2123
}
2224

@@ -49,4 +51,8 @@ export class HeroDetailPage implements OnInit {
4951
goBack(): void {
5052
this.location.back();
5153
}
54+
55+
goToTheAnchor(): void {
56+
this.router.navigate([`/${AppConfig.routes.heroes}/${this.hero.id}`], {fragment: 'heroe-detail'});
57+
}
5258
}

0 commit comments

Comments
 (0)