Cannot set variable from subscribe in router events
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Performance issue
[ ] 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
[ ] Other... Please describe:
Current behavior
ngOnInit() {
this.prevRoute = 'init';
this.router.events.pipe(
filter((e: any) => e instanceof RoutesRecognized),
pairwise(),
map((lastRoutes: RoutesRecognized[]) => lastRoutes[0].urlAfterRedirects),
first(),
).subscribe(prevRoute => {
this.prevRoute = prevRoute; // e.g. = '/prev_route'
});
console.log(this.prevRoute); // here gives 'init' once again.
...
}
this.prevRoute
is set inside subscribe but once it ‘goes out’ of the subscribe the value is forgotten.
Expected behavior
Be able to set outer scope variables from subscribe
.
Minimal reproduction of the problem with instructions
Use code from above.
What is the motivation / use case for changing the behavior?
This supposed to be possible.
Environment
develop (after ng serve)
"@angular/animations": "^6.1.7",
"@angular/cdk": "^6.4.7",
"@angular/common": "^6.1.7",
"@angular/compiler": "^6.1.7",
"@angular/core": "^6.1.7",
"@angular/forms": "^6.1.7",
"@angular/http": "^6.1.7",
rxjs@^6.3.2,
Browser:
- Chrome (desktop) version 69.0.3497.92
- Chrome (Android) version XX
- Chrome (iOS) version XX
- Firefox version XX
- Safari (desktop) version 11.2
- Safari (iOS) version XX
- IE version XX
- Edge version XX
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Updating variable from within event subscription in Angular
Updating variable from within event subscription in Angular ; from '@angular/core' ; Router, NavigationEnd ; '@angular/router'; import ; 'rxjs/ ...
Read more >Parameterised Routes • Angular - codecraft.tv
With parameterised routes we can support variable paths in our routes. ... How to configure parameterised routes in our route definition object.
Read more >Can't assign value to variable using subscribe() method in ...
The simplest way is to move to code into subscribe(...) constructor (private dataService: DataService){ dataService.getCompaniesCount().subscribe(res => { this.
Read more >RouterOutlet - Angular
A router outlet emits an activate event when a new component is instantiated, deactivate event when a component is destroyed. An attached event...
Read more >48 answers on StackOverflow to the most popular Angular ...
How to detect a route change in Angular? Global Events in Angular; What are differences between SystemJS and Webpack? Angular: Can't find ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This has nothing to do with angular
Hello, we reviewed this issue and determined that it doesn’t fall into the bug report or feature request category. This issue tracker is not suitable for support requests, please repost your issue on StackOverflow using tag
angular
.If you are wondering why we don’t resolve support issues via the issue tracker, please check out this explanation.
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.