我的用户可以在网址栏中更改日期。
网址更改的逻辑在我的app.js =>中stateChangeStart function =>
.run(['$rootScope', '$state', '$stateParams', 'plannerFactory',
function ($rootScope, $state, $stateParams, plannerFactory) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
$rootScope.$on('$stateChangeStart', function (ev, toState, toParams, fromState, fromParams) {
if (toState.name === "main.projects.selected.dates.day") {
ev.preventDefault();
// code here removed for brevity...
var dateString = toParams.year + " " + toParams.month + " " + toParams.day;
var newDate = moment(dateString, "YYYY MM DD");
if (newDate.isValid()) {
plannerFactory.day = newDate.subtract(1, 'months');
}
else {
toState.name = "main.projects";
// When this is done and I am on the main.projectsstate I want to return HERE again entering a correct date to execute
// the above code where the date is valid BUT before I get here again I do the $state.go(...) which always worked but NOW
// the toState.name has STILL the old main.projects, I assumed the $state.go which I do would change the toState.name not?
// That would make sense...
}
// This prevents infinite loop/recursion
$state.go(toState.name, toParams, { notify: false }).then(function () {
$rootScope.$broadcast('$stateChangeSuccess', toState, toParams, fromState, fromParams);
});
}
}
当网址更改未导致有效日期时,我想将用户重定向到main.projects网站,这是我的登陆/开始页面。
当我被重定向到main.projects状态时,我点击一个触发$ state.go的按钮(' main.projects.whatever');
该项目控制器中的state.go'我通过以下方式打开一个项目:
$scope.open = function () {
plannerFactory.day = moment();
$state.go("main.projects.selected.dates.day", { id: $state.params.id }, {reload: true});
};
触发打开的项目按钮然后触发stateChangeStart,当我检查toState.name时,我可以读取值' main.projects'但实际上,从逻辑上讲,toState.name是' main.projects.whatever'。由于情况并非如此,我从未达到目标,而是有一种递归/恢复行为。
我永远不能离开main.projects状态。
这是一个错误还是我错了什么?
更新