UI路由器刷新页面代替状态更改 [英] UI Router Refreshing Page Instead on State Change

查看:88
本文介绍了UI路由器刷新页面代替状态更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个特殊的问题没有运气.我所查找的所有内容均与尝试"刷新页面的问题有关,而不是每次进入某种状态时都会刷新的问题.这仅发生在IE中而不是Chrome中.

I've had no luck with this particular issue. Everything I've looked up has to do with people having issues 'trying' to refresh the page instead of my issue where it's refreshing every time I go to a certain state. This ONLY happens in IE not chrome.

代码:

app.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('box', {
            templateUrl: templatePath + 'box.html',
            controller: 'BoxController',
            controllerAs: 'box',
            url: '/Box/{folder:string}'
        })
        .state('compose', {
            templateUrl: templatePath + 'compose.html',
            controller: 'ComposeController',
            controllerAs: 'compose',
            url: '/Compose/{messageSentId:int}'
        })
        .state('view', {
            templateUrl: templatePath + 'view.html',
            controller: 'ViewController',
            controllerAs: 'view',
            url: '/Box/{folder:string}/{messageId:string}/{messageSentId:string}'
        });

    $urlRouterProvider.otherwise('/Box/');
});

问题出在我的 compose 状态.如果我这样直接进入该状态:

The problem is with my compose state. If I go directly to that state like so:

vm.compose = function () {
   $state.go('compose', { messageSentId: 0 }, { notify: true });
};

效果很好.但是,如果我添加除0之外的其他数字并从其他controller呼叫,则无效:

It works just fine. However, if I add a different number besides 0 and call from a different controller it does not work:

vm.reply = function() {
   var id = Math.floor($stateParams.messageSentId);
   $state.go('compose', { messageSentId: id }, { notify: true });
};

我认为可能是有问题,因为它认为id是字符串,因此与/Compose/{messageSentId:int}不匹配,所以我添加了Math.floor(),但这没有帮助.

I thought maybe it was having issues because it thought id was a string and therefore didn't match to /Compose/{messageSentId:int} so I added the Math.floor() but that didn't help.

需要注意的是,如果我先启动我的compose函数并进入该状态,则reply函数将起作用.但是,如果我尝试使用回复功能进行导航,则会重新加载页面.

Something to note is that if I fire my compose function first and go to that state the reply function will work. However, if I attempt to navigate with my reply function first the page reloads.

我可以确认的另一件事是该页面的controller和页面本身加载正常.您实际上可以看到表格短暂弹出.问题是一旦controller已加载刷新,便会触发.没有错误.没什么.只是在IE中失败.

Another thing that I can confirm is that my controller for that page and the page itself loads just fine. You can actually see the form pop up briefly. The problem is once the controller has loaded a refresh is triggered. No errors. No nothing. Simply fails in IE.

推荐答案

经过数小时的研究,没有发现与网上问题相似的东西,我做了一个简单的更改.我将状态更改从控制器中移出,改用ui-sref:

After many hours of research and having found nothing similar to my issue on the web I made a simple change. I moved my state change out of my controller and used ui-sref instead:

代码:

<div class="btn-group pull-right" ng-if="view.canReply()">
    <a class="btn btn-primary" ui-sref="compose({messageSentId: view.replyMessageId})">
        <i class="fa fa-reply"></i> Reply
    </a>
</div>

这里的视图"是我的控制器,因为我不使用$ scope,而是在变量ReplyMessageId上设置了$ stateparameter.也许这会帮助某人.

Where 'view' is my controllerAs since I don't use $scope and I set my $stateparameter on the variable replyMessageId. Maybe this will help someone.

这篇关于UI路由器刷新页面代替状态更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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