Angularjs 如何在ui路由器stateChangeStart函数中注入自定义数据(如factory)
我称之为:Angularjs 如何在ui路由器stateChangeStart函数中注入自定义数据(如factory),angularjs,angular-ui-router,Angularjs,Angular Ui Router,我称之为: $state.go("main.projects.selected.dates.day", { id: $state.params.id }, {reload: true}); 我确实希望将自定义数据从state.go函数传递到stateChangeStart函数。这是可能的状态参数-我可以滥用它接受一个日期对象-但我的url将不再正常工作 如何从state.go到stateChangeStart传递数据?仅仅通过黑客攻击rootScope.customData属性 $rootSc
$state.go("main.projects.selected.dates.day", { id: $state.params.id }, {reload: true});
我确实希望将自定义数据从state.go函数传递到stateChangeStart函数。这是可能的状态参数-我可以滥用它接受一个日期对象-但我的url将不再正常工作
如何从state.go到stateChangeStart传递数据?仅仅通过黑客攻击rootScope.customData属性
$rootScope.$on('$stateChangeStart', function (ev, toState, toParams, fromState, fromParams, injectCustomFactory here)
{
// Run custom logic here on the injected custom factory
// then change the toParams.customData property by assigning the injected value to this property.
}
您可能会问,为什么我必须在stateChangeStart中运行此逻辑
这是因为只有在那里,我才能检查用户是否主动更改了url($rootScope.customData是falsy),还是在我执行$rootScope.customData=MyValueLogic之前从外部执行state.go 将其注入到运行中(或您正在观看的任何位置$stateChangeStart
):
angular.module('lalala').run(function ($rootScope, factory) {
$rootScope.on('$stateChangeStart', function (e, toState, ...) {
factory.doSomething();
});
});