gpt4 book ai didi

javascript - 如何从 ui-router statechange 返回 $state.current.name

转载 作者:IT王子 更新时间:2023-10-29 03:14:54 26 4
gpt4 key购买 nike

当我改变 Angular 位置时,我想返回 .state('name')

从我的 run() 它可以返回 $state 对象:

 .run(function($rootScope, Analytics, $location, $stateParams, $state) {
console.log($state);

working object

但是当我尝试获取$state.current时它是空对象

.run(function($rootScope, $location, $stateParams, $state) {

console.log($state.current);

empty

配置示例:

 .config(function($stateProvider, $urlRouterProvider, AnalyticsProvider) {  
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
views: {
'': {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
},
'navigation@home': {
templateUrl: 'views/partials/navigation.html',
controller: 'NavigationCtrl'
},
'weekly@home': {
templateUrl: 'views/partials/weekly.html',
controller: 'WeeklyCtrl'
},
'sidepanel@home': {
templateUrl: 'views/partials/side-panel.html',
controller: 'SidePanelCtrl'
},
'shoppanel@home': {
templateUrl: 'views/partials/shop-panel.html',
controller: 'ShopPanelCtrl'
},
'footer@home': {
templateUrl: 'views/partials/footer.html',
controller: 'FooterCtrl'
}
}
})

最佳答案

您可以监听“$stateChangeSuccess”并相应地设置状态。例如-

$rootScope.$on('$stateChangeSuccess',
function(event, toState, toParams, fromState, fromParams) {
$state.current = toState;
}
)

关于javascript - 如何从 ui-router statechange 返回 $state.current.name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24895908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com