admin管理员组文章数量:1026989
There is a global event that we can use when state change/start that not per ponent unlike the Component Lifecycle Hooks ? like in UI-router:
$rootScope.$on("$stateChangeStart", function() {})
There is a global event that we can use when state change/start that not per ponent unlike the Component Lifecycle Hooks ? like in UI-router:
$rootScope.$on("$stateChangeStart", function() {})
Share
Improve this question
edited Feb 11, 2019 at 3:46
user10747134
asked Dec 21, 2015 at 7:29
user233232user233232
6,2179 gold badges30 silver badges37 bronze badges
2 Answers
Reset to default 1It depends on what you want to achieve, but it is possible to inject Router
in your top level ponent and .subscribe()
to it to get the stream of states.
I used it to build functionality that changes browser's title based on current state. That being said you can think about it as equivalent of $stateChangeSuccess
and $stateChangeFailure
events from Angular 1.
The code will be:
constructor(router: Router) {
router.subscribe(successHandler, failureHandler);
}
Also take a look on OnActivate which is also related to these concepts.
My code, for ui-router, has ended up looking something like the following to replace ng1 $rootScope $stateChangeSuccess for Angular2:
import { Component } from '@angular/core';
import { TransitionService } from "ui-router-ng2";
@Component({selector: 'app-stage-tag',template: '...'})
class AppComponent {
constructor(public transitionService: TransitionService){
transitionService.onSuccess({to:'*'}, transition=>{
console.log('state', transition._targetState._definition)
console.log('params', transition._targetState._params)
})
}
}
There is a global event that we can use when state change/start that not per ponent unlike the Component Lifecycle Hooks ? like in UI-router:
$rootScope.$on("$stateChangeStart", function() {})
There is a global event that we can use when state change/start that not per ponent unlike the Component Lifecycle Hooks ? like in UI-router:
$rootScope.$on("$stateChangeStart", function() {})
Share
Improve this question
edited Feb 11, 2019 at 3:46
user10747134
asked Dec 21, 2015 at 7:29
user233232user233232
6,2179 gold badges30 silver badges37 bronze badges
2 Answers
Reset to default 1It depends on what you want to achieve, but it is possible to inject Router
in your top level ponent and .subscribe()
to it to get the stream of states.
I used it to build functionality that changes browser's title based on current state. That being said you can think about it as equivalent of $stateChangeSuccess
and $stateChangeFailure
events from Angular 1.
The code will be:
constructor(router: Router) {
router.subscribe(successHandler, failureHandler);
}
Also take a look on OnActivate which is also related to these concepts.
My code, for ui-router, has ended up looking something like the following to replace ng1 $rootScope $stateChangeSuccess for Angular2:
import { Component } from '@angular/core';
import { TransitionService } from "ui-router-ng2";
@Component({selector: 'app-stage-tag',template: '...'})
class AppComponent {
constructor(public transitionService: TransitionService){
transitionService.onSuccess({to:'*'}, transition=>{
console.log('state', transition._targetState._definition)
console.log('params', transition._targetState._params)
})
}
}
本文标签: javascriptAngular 2 router global state change eventStack Overflow
版权声明:本文标题:javascript - Angular 2 router global state change event - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745661707a2161929.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论