如何在角度中触发$ stateChangeStart?

时间:2016-02-18 09:35:11

标签: angularjs routes angular-ui-router

如果我这样写:

$window.confirm

总是要求确定或取消,我不想使用$scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { if ($scope.isChanged) { var isjump = confirm('do you want leave?'); if (!isjump) { event.preventDefault(); } } });

{{1}}

1 个答案:

答案 0 :(得分:0)

我认为如果用户想要离开,您想重置$scope.isChanged。然后,当您致电$state.go()时,条件不会导致警告显示

$scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
    if ($scope.isChanged) {
        myShowOkCancelDialog(NotifyType.Warn, 'do you want leave?', function (ok) {
            if (ok) {
                $scope.isChanged = false;
                // $stateChangeStart will not enter `if` now
                $state.go(toState, toParams);
            }
        })
        event.preventDefault();
    }
});