question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

router subscription executed inside of Angular zone even when wrapped in runOutsideAngular()

See original GitHub issue

I’m submitting a …

[x] bug report
[ ] feature request
[ ] support request

Current behavior It seems that runOutsideAngular() is not working consistently in combination with routing.

If I surround a subscription to ActiveRoute.params with ngZone.runOutsideAngular() within a component, it will run outside of the Angular zone for the initial navigation, but return to the zone for subsequent router navigations.

Expected behavior If I wrap any subscription in ngZone.runOutsideAngular(), I would expect the code to run outside of zone consistently.

Minimal reproduction of the problem with instructions Minimal working example: http://plnkr.co/edit/bg3nuimPGNppsF5wX20h?p=preview

There is one default redirection in the routes to ‘/foo’ and one manually in the SubComponent to ‘/foo2’.

The console output shows the strange behaviour:

param: foo, run outside of zone: true
param: foo2, run outside of zone: false // <-- I would expect this to be true!

The relevant code is in sub.component.ts:

@Component({
  template: 'RouteParam: {{ activeRoute }}'
})
export class SubComponent {
  public activeRoute: string;

  constructor(private route: ActivatedRoute, private ngZone: NgZone, private router: Router) {
    
    this.ngZone.runOutsideAngular(() => {
      route.params
        .map(params => params['routeParam'])
        .do(param => console.log('param: ' + param + ', run outside of zone: ' + !NgZone.isInAngularZone()))
        .subscribe(param => this.activeRoute = param);
      
      this.router.navigate(['foo2']);
    });

  }
}

What is the motivation / use case for changing the behavior? I want to combine the params with HTTP polling, so I need the subscription to run outside of the ngZone or protractor will time out. (see http://stackoverflow.com/questions/36358405/how-to-implement-intervals-polling-in-angular2-to-work-with-protractor)

Please tell us about your environment: Windows 10, Angular CLI 1.0.0-rc.1

  • Angular version: reproduced with 2.4.0 and 4.0.2

  • Browser: all

  • Language: TypeScript 2.0.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
JiaLiPassioncommented, Dec 20, 2017

@jasonaden, , this one has been closed https://github.com/ReactiveX/rxjs/pull/2266. @magnattic, and now zone.js has a new patch for rxjs, you can try to

import 'zone.js/dist/zone-patch-rxjs';

in polyfill after import zone

0reactions
angular-automatic-lock-bot[bot]commented, Sep 13, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Navigation triggered outside Angular zone, did you forget to ...
I have a router.navigate() call inside a subscribe() method block. When you say to put it outside, where exactly would that be ?...
Read more >
NgZone - Angular
A zone is an execution context that persists across async tasks. You can think of it as thread-local storage for the JavaScript VM....
Read more >
How `runOutsideAngular` might reduce change detection calls ...
Let's say we have to do some canvas animation inside our angular app. Our animation loop is simple setInterval . this.interval = window....
Read more >
Using Zones in Angular for better performance
runOutsideAngular() . All we have to do is to make sure that the mouseMove() event handler is only attached and executed outside Angular's...
Read more >
Angular 2 Change Detection (part 2) - Gofore
As with other approaches, the change detection in Angular 2 wraps around solving ... This function will be executed inside the current zone....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found