Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 event when any [routerLink] is clicked

I have a simple Loader Service that hides and shows certain loaders. I'm working on something that will be used a lot with slow connections and I need to show/hide a loader between route changes.

I can hide the loader when the new route is loaded with the following.

this._Router.subscribe(() => {
    this._LoaderService.hide();
})

I'm trying to find a way that I can call my this._LoaderService.show() function immediately when any [routerLink] is clicked (at the start of the route change, not the end).

I've had a look around and I tried https://angular.io/docs/ts/latest/api/router/Router-class.html but I can't seem to figure it out.

Any help appreciated

like image 297
Oliver Millington Avatar asked Mar 25 '16 03:03

Oliver Millington


People also ask

How do I know if my routerLink is active?

routerLinkActive is simply an indicator of whether this link represents the active route. @jessepinho - Tried it - [routerLinkActive]="'active'" will only work if you also have [routerLink] in the a-tag. If you had (click)="navitageTo('/route')" instead of [routerLink], and in that function, you called this.

What is difference between routerLink and routerLink?

What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.

What does routerLink do in angular?

RouterLink is a built-in Angular Directive that lets you link to specific routes in your app. In the SPA(single-page application), you change what the user sees by showing or hiding portions of the display that correspond to particular components, rather than going out to the server to get a new page.

Does routerLink work on Div?

Yes it can be attached to div tag, your route is probably wrong try add / in front of route.


3 Answers

With this much information it is not possible to tell you correct way, please also note this that loader service implementation varies according to your router implementation.

Either you have to extend route-outlet class and should handle loaderService there OR you have to handle link's click by your own like,

<a  [routeLink]=['User']>User</a>

chage it to,

<a (click)="changeRoute('User')">User</a>

then,

import { Router } from '@angular/router';

constructor(private router: Router){}

changeRoute(routeValue) {
   this._LoaderService.show(); 
   //this will start the loader service.

   this.router.navigate([routeValue]); 
   // you have to check this out by passing required route value.
   // this line will redirect you to your destination. By reaching to destination you can close your loader service.
   // please note this implementation may vary according to your routing code.

}

And when a particular route/link/component becomes active, within that component you can close your loader service whenever and whereever you want to.

like image 71
Nikhil Shah Avatar answered Oct 24 '22 22:10

Nikhil Shah


You could create your own routerLink directive by extending the default one https://github.com/angular/angular/blob/master/packages/router/src/directives/router_link.ts and override the onClick()

Similar to micronyks answer

 <a  [routeLink]=['User'] (click)="loaderService.show()">User</a>

should work as well but you have to add the click handler everywhwere.

like image 35
Günter Zöchbauer Avatar answered Oct 24 '22 23:10

Günter Zöchbauer


I had quite similar need when integrating bootstrap navbar into Angular2 app.

The solution I came up was binding a boolean variable to control the toggle state of the navbar (equivalent to hide and show on your case).

Once the user clicks on the hamburger icon or the unordered list of the routerLinks, the variable was set accordingly. Note I bind the click event on the parent of the anchors that actually do the routing in Angular.

Looks more convenient then extending the routerLink directive.

<nav class="navbar navbar-default navbar-custom {{isfixed}} {{istransparent}}" *ngIf="showNav" aria-expanded="false">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header page-scroll">
                <button  (click)="isCollapsed = !isCollapsed" type="button" class="navbar-toggle collapsed">
                    <span class="sr-only">Toggle navigation</span>
                    <i class="fa fa-bars"></i>
                </button>
                <a routerLink="/home" routerLinkActive="active" class="navbar-brand">
                    <span><img src="assets/icon/android-icon-36x36.png"></span>
                    {{brandText}}
                </a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"
            [attr.aria-expanded]="!isCollapsed" [ngClass]="{collapse: isCollapsed}">
                <ul class="nav navbar-nav navbar-right" (click)="isCollapsed = !isCollapsed">
                    <li>
                        <a routerLink="/home" routerLinkActive="active">Home</a>
                    </li>
                    <li>
                        <a routerLink="/about" routerLinkActive="active">About</a>
                    </li>
                    <li>
                        <a routerLink="/posts" routerLinkActive="active">Posts</a>
                    </li>
                    <li>
                        <a routerLink="/contact" routerLinkActive="active">Contact</a>
                    </li>
                </ul>
            </div> <!-- /.navbar-collapse -->
        </div> <!-- /.container -->
</nav> <!-- NAV -->
like image 38
Yamada Avatar answered Oct 24 '22 23:10

Yamada