确认后 $state.go 不会改变角度路线 [英] $state.go won't change route in angular after confirmation

查看:20
本文介绍了确认后 $state.go 不会改变角度路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码可以使用 angular-ui 路由器以角度确认导航:

I have this code to confirm navigation away in angular, using angular-ui router:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
        //checks if campaign has been modified without save. 
        //if yes state change needs to be confirmed by modal
        //otherwise simple navigates away
        event.preventDefault();
        // var _this = this;
        _this.toState = toState;
        _this.toParams = toParams;

        var modalInstance = bootstrapModals.confirm({
            title: "Exit without saving?",
            message: "You have changes to the campaign. Leave without saving?"
        });
        modalInstance.then(function(result) {
            $state.go(_this.toState.name, _this.toParams);
        }, function(error) {
            event.preventDefault();
        });

    });

然而 $state.go 只再次显示模态,没有任何路线改变._this.toState.name 和 _this.toParams 中的值是正确的.

However $state.go only displays the modal again, without any route change. The values in _this.toState.name and _this.toParams are correct.

有什么问题?

推荐答案

成功是无限循环的.每次进行状态更改时,都会返回代码,导致再次显示模态.您需要保存答案以防止模态再次弹出.

You have an infinite loop on success. Every time you do a state change, you go back in the code causing the modal to be displayed again. You need to save the answer to prevent the modal from popping back up.

这篇关于确认后 $state.go 不会改变角度路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆