UI-路由器的决心功能仅被调用一次 [英] UI-Router's resolve functions are only called once

查看:176
本文介绍了UI-路由器的决心功能仅被调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用的用户界面的路由器解决功能注入一些readiliy解决的承诺进入我的控制器。

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

我使用的示例plnkr做出了榜样。
考虑到这些嵌套的状态: ROUTE1 route1.list
我有一个名为 ABC 下决心功能上 ROUTE1 定义。现在,当我浏览到 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.

<一个href=\"http://plnkr.co/edit/mi5H5HKVAO3J6PCfKSW3?p=$p$pview\">http://plnkr.co/edit/mi5H5HKVAO3J6PCfKSW3?p=$p$pview

我想这是故意的,但考虑到该用例:
决心功能通过HTTP检索一些数据,应该刷新/在某个时候失效,也许每一个状态变化。是否有某种方式使用嵌套的状态时,要做到这一点?
如果解析函数被调用每一个注入我可以实现任何方式我想:返回旧,解决承诺或创建一个新的

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.

推荐答案

直供选项重载:真的去() / transtionTo() / UI-SREF 的伎俩:-)
由于的设计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.

重装记录如下

重装(v0.2.5) - {布尔= FALSE},如果属实,将迫使过渡即使国家或PARAMS没有改变,又名相同的状态重新加载。它不同于reloadOnSearch因为你用这个当你要强制时,一切都是一样的,包括搜索PARAMS重载。

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 链接变成这样:&LT;一个UI的SREF =ROUTE1用户界面 - SREF-OPTS ={重装:真正}&GT;

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

要避免在每一个环节提供的选择,我写周围的 $状态(也是<看到一个装饰href=\"http://plnkr.co/edit/mi5H5HKVAO3J6PCfKSW3?p=$p$pview\">http://plnkr.co/edit/mi5H5HKVAO3J6PCfKSW3?p=$p$pview):

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-路由器的决心功能仅被调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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