UI-Router 的解析函数只调用一次 [英] UI-Router's resolve functions are only called once

查看:23
本文介绍了UI-Router 的解析函数只调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用 ui-routers 解析功能将一些容易解析的 promise 注入到我的控制器中.

I was going to use ui-routers resolve feature to inject some readiliy resolved promises into my controllers.

我用例子plnkr来做一个例子.考虑以下嵌套状态:route1route1.list.我在 route1 上定义了一个名为 abc 的解析函数.现在,当我第一次导航到 route1 时,会调用 abc 并将被解析.现在,当我导航到 route1.list 并返回到 route1 时,不会再次调用 abc.

I used the example plnkr to make an example. Consider these nested states: route1 and route1.list. I have a resolve function called abc defined on route1. Now when I navigate to route1 for the first time, abc is called and will be resolved. Now when I navigate to route1.list and back to route1, abc is not called again.

http://plnkr.co/edit/mi5H5HKVAO3J6PCfKSW3?p=preview

我想这是故意的,但请考虑以下用例:解析函数通过 http 检索一些数据,并且应该在某个时候刷新/失效,可能是在每次状态更改时.使用嵌套状态时有什么方法可以做到这一点吗?如果在每次注入时都调用了 resolve 函数,我可以以任何我想要的方式实现它:返回旧的、已解决的 promise 或创建一个新的 promise.

I guess this is intentional, but consider this use-case: the resolve function retrieves some data via http and should be refreshed/invalidated at some point, maybe on every state change. Is there some way to do this when using nested states? If the resolve function was called on every inject I could implement it any way I want: Return the old, resolved promise or create a new one.

我只是简单地测试了这个,但是如果状态不是嵌套的,事情就会按我预期的那样工作.放弃嵌套状态因为这很糟糕.在嵌套状态下使用解析依赖项实际上很好.

I have only briefly tested this, but if the states were not nested things would work as I expected. Giving up on nested states because of this stinks though. And having the resolve dependencies available in nested states is actually nice.

推荐答案

提供选项 reload:truego()/transtionTo()/ui-sref 可以解决问题:-)感谢 Designing the Code 为我指明了这个方向.不过解决方案有点不同,所以我写了这个答案.

Supplying the option reload:true to go() / transtionTo() / ui-sref does the trick :-) Thanks to Designing the Code for pointing me in this direction. The solution is a bit different though, so I write up this answer.

重新加载记录如下:

reload (v0.2.5) - {boolean=false},如果 true 会强制转换,即使状态或参数没有改变,也就是重新加载相同的状态.它与 reloadOnSearch 不同,因为当你想在一切都一样的情况下强制重新加载时,你会使用它,包括搜索参数.

reload (v0.2.5) - {boolean=false}, If true will force transition even if the state or params have not changed, aka a reload of the same state. It differs from reloadOnSearch because you'd use this when you want to force a reload when everything is the same, including search params.

直接的方法是将每个 ui-sref 链接更改为如下所示:<a ui-sref="route1" ui-sref-opts="{reload: true}">.

The direct way is to change every ui-sref link into something like this: <a ui-sref="route1" ui-sref-opts="{reload: true}">.

为了避免在每个链接上都提供选项,我围绕 $state 编写了一个装饰器(另请参阅 http://plnkr.co/edit/mi5H5HKVAO3J6PCfKSW3?p=preview):

To avoid supplying the option at every link I wrote a decorator around $state (see also http://plnkr.co/edit/mi5H5HKVAO3J6PCfKSW3?p=preview):

 myapp.config(function($provide) {
  $provide.decorator('$state', function($delegate) {
    var originalTransitionTo = $delegate.transitionTo;
    $delegate.transitionTo = function(to, toParams, options) {
      return originalTransitionTo(to, toParams, angular.extend({
        reload: true
      }, options));
    };
    return $delegate;
  });
});

这篇关于UI-Router 的解析函数只调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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