如何在转换开始期间找到目标状态

时间:2018-06-04 22:17:28

标签: angularjs angular-ui-router

如何在开始转换时找到下一个状态。我已经编写了代码来显示启动模式,以便在表单变脏时向用户显示消息,但是当转换到某些状态时,我甚至不希望显示此消息,即使表单是脏的。有没有我可以注入的服务来找到我正在过渡的下一个州。

$transitions.onStart({}, () => {

  //here I want to add a condition to not launch this modal if the $state name is xyz, otherwise just show the modal

  if (($scope.form.$dirty)) {
                return $uibModal.open({
                    templateUrl: '/app/shared/modalDialog.html',
                    controller: 'ModalDialogController',
                    controllerAs: 'modal',
                    resolve: {
                        modalOptions: function () {
                            return {
                                headerText: 'Unsaved Changes',
                                bodyText: 'There are unsaved changes on this page. Are you sure you want to leave?',
                                primaryText: 'Continue',
                                secondaryText: 'Cancel'
                            };
                        }
                    }
                }).result
                 .then(result => {
                        if (result === 'secondary')
                        {
                            return false;
                        }
                    });
}
}

1 个答案:

答案 0 :(得分:0)

您可以利用angular-ui-router广播各种状态事件(包括$stateChangeStart)的事实。我们有一个全局控制器,我们为这个事件注册一个处理程序:

$scope.$on('$stateChangeStart', (
    event,
    toState,
    toParams,
    fromState,
    fromParams) => {
        // code goes here to examine to the 'toState' parameter
        // and take any appropriate action
    });