gpt4 book ai didi

javascript - Angularjs ui-router,如何在 $stateChangeSuccess 触发时获取预加载解析值?

转载 作者:搜寻专家 更新时间:2023-11-01 04:51:54 24 4
gpt4 key购买 nike

$stateProvider.state('home', {
url: '/',
resolve: {
person: function() {
return 'good'
}
}

像上面的状态配置一样,如何在 $stateChangeSuccess 回调函数中获取 'person' 值?

$rootScope.$on('$stateChangeSuccess', 
function(event, toState, toParams, fromState, fromParams) {
// I want get the 'person' value in this function, what should I do?
});

最佳答案

我们遇到了同样的问题。我们是依靠ui-router的一些内部实现细节来解决的;这确实意味着它可能会在未来版本的 Angular 中中断,但这是我们使用的函数:

function annotatedStateObject(state, $current) {
state = _.extend({}, state);
var resolveData = $current.locals.resolve.$$values;
state.params = resolveData.$stateParams;
state.resolve = _.omit(resolveData, '$stateParams');
state.includes = $current.includes;
return state;
}

这将获取参数、包含和所有(已解析的)解析对象。我们像这样在回调中使用它:

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

var person = toState.resolve.person;
}

希望对您有所帮助!

关于javascript - Angularjs ui-router,如何在 $stateChangeSuccess 触发时获取预加载解析值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24634119/

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