Skip to content

Feature Request: Add a delay option to routerLink #17485

Closed

Description

@Pittan

I'm submitting a ...


[ ] Regression (behavior that used to work and stopped working in a new release)
[ ] Bug report 
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Expected behavior

When after clicked routerLink, I want to delay the transition. Like....

setTimeout(() => {
  this.router.navigateByUrl(this.urlTree, extras)
}, 220)

What is the motivation / use case for changing the behavior?

I have a sidebar menu in my Angular App.
iOS Safari has swipe back feature.
When using it, it shows latest snapshot on previous screen.
This behavior is really annoying because the menu looks like it's flickering.
I want to delay the transition and close the menu first.

Activity

  1. dblVs commented on Jun 14, 2017

    @dblVs

    Isn't that just resolving the state whenever that animation finishes?
    here is a link to the docs that tell you how to do it. In your case that would be

    new Promise((res) => {
      setTimeout(() => {
        this.router.navigateByUrl(this.urlTree, extras);
        res();
      }, 220);
    })
    

    NOTE
    Consider using animation frames rather than timeout. That makes sure your animation will finish before the transition whereby timeout doesn't. Also removes the need of keeping that value in sync with your css.

  2. Pittan commented on Jun 14, 2017

    @Pittan
    Author

    @dblVs
    I'm sorry I didn't make it clear enough.
    I need to use with anchor tag. Like
    <a routerLink="/awesome/{{awesome.id}}">My awesome page</a> .
    I can't use router.navigate in my own ts files.

    The code I wrote in my expected behavior is about routerLink directive in angular/core.

  3. dblVs commented on Jun 14, 2017

    @dblVs

    It doesn't matter where you enter the state from. As long as you have done the resolve it should work. And also RouterLink is in @angular/router. (I had similar issue and that's how I fixed it. Check documentation for Resolve on angular.io)

  4. tytskyi commented on Jun 14, 2017

    @tytskyi

    routerLink on <a> tag builds correct full URL for open-in-new-tab

  5. jasonaden commented on Jun 27, 2017

    @jasonaden
    Contributor

    @Pittan It sounds like using a Resolver like @dblVs has suggested would be a good solution. If you were to add the timeout at the RouterLink side, what happens when the user changes router state through the URL? You won't have the same delay and things will look wrong.

    If you want to wait, using the Resolver will work in either case.

  6. briancodes commented on Mar 8, 2018

    @briancodes

    @Pittan I had the same problem with a menu close transition flickering on navigation. It made the page transitions look very laggy

    I extended the routerLink href directive to add in a delay with Observable.timer() - with an additional input [navigationDelay]="150" which you put on the <a> element. I uploaded it on npm as an Angular module that includes the extended routerLink and routerLinkActive directives - you can see how it works there

  7. Pittan commented on Mar 10, 2018

    @Pittan
    Author

    @briancodes Thanks Brian! This npm module looks great! I'll try the module with my Angular application later.

  8. tkstang commented on Jun 25, 2018

    @tkstang

    I resolved a similar issue by creating a DelayResolve which I applied to my routes.

    import { Observable } from 'rxjs';
    import { Injectable} from '@angular/core';
    import { Resolve } from '@angular/router';
    
    @Injectable()
    export class DelayResolve implements Resolve<Observable<any>> {
    
      constructor() {
      }
    
      resolve(): any {
        return Observable.of('delayed navigation').delay(500);
      }
    }
    

    Then it the routes just apply the resolve:

    path: 'yourpath', component: YourComponent, resolve: [DelayResolve]

  9. dblVs commented on Jun 26, 2018

    @dblVs

    @tkstang you could have also used

    import { Observable } from 'rxjs/Observable';
    
    ...
     
    public resolve(): Observable<number> {
     return Observable.timer(1000);
    }
    

    or even better with rxjs 6

    import { Observable, timer } from 'rxjs';
    
    ...
    
    public resolve(): Observable<number> {
     return timer(1000);
    }
    
  10. angular-automatic-lock-bot commented on Sep 13, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  11. locked and limited conversation to collaborators on Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions