角 - UI路由器 - 编程方式添加国家 [英] Angular - UI Router - programmatically add states

查看:183
本文介绍了角 - UI路由器 - 编程方式添加国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,以模块配置后状态编程方式添加到$ stateProvider,在如服务?

要加入更多内容,这个问题,我有一个情况我可以用两种方法去:


  1. 尝试强行打开在模块配置中定义的状态重装,问题是,国家有 reloadOnSearch 设置为,所以当我尝试的 $ state.go('state.name',{新:参数},{重装:真}); 的什么也没有发生,任何想法

状态定义

  .STATE('index.resource.view',{
  网址:/:资源名称/视图PAGENUMBER和放大器; pageSize的&安培; ORDERBY&安培​​;搜索
  templateUrl:/resourceAdministration/views/view.html
  控制器:resourceViewCtrl
  reloadOnSearch:假的,
})

<醇开始=2>

  • 尝试以编程方式补充一点,我需要从服务加载,以便路由能正常工作状态。我宁愿去与第一选择,如果可能的。


  • 解决方案

    通常状态在配置阶段添加到 $ stateProvider 。如果你想在运行时添加的状态,你需要不断的 $ stateProvider 来的一个参考各地。

    这code是未经测试,但应该做你想要什么。它创建了一个名为服务 runtimeStates 。你可以注射到运行code,然后添加状态。

      //配置时的依赖可以在这里.provider注入()声明
    myapp.provider('runtimeStates',功能runtimeStates($ stateProvider){
      //对于服务运行时依赖这里可以注射,在提供者。$得到()函数。
      这一点。$ GET =功能($ Q $超时,$州){//例如
        返回{
          addState:函数(名称,状态){
            $ stateProvider.state(名称,状态);
          }
        }
      }
    });

    我已经实现了一些东西叫未来美国 ,路由器额外该照顾的一些极端案例以你喜欢的网址映射到还不存在的状态。未来国家也显示了如何能延迟加载源$ C ​​$ C运行时态。看看在<一个href=\"https://github.com/christopherthielen/ui-router-extras/blob/master/src/futureState.js#L165\">source code ,以获得所涉及的感觉。

    Is there a way to programmatically add states to $stateProvider after module configuration, in e.g. service ?

    To add more context to this question, I have a situation where I can go with two approaches:

    1. try to force the reload on the state defined in the module configuration, the problem is that state has a reloadOnSearch set to false, so when I try $state.go('state.name', {new:param}, {reload:true}); nothing happens, any ideas ?

    State definition

    .state('index.resource.view', {
      url: "/:resourceName/view?pageNumber&pageSize&orderBy&search",
      templateUrl: "/resourceAdministration/views/view.html",
      controller: "resourceViewCtrl",
      reloadOnSearch: false,
    })
    

    1. try to programmatically add states that I need to load from a service so routing can work properly. I'd rather go with the first option if possible.

    解决方案

    Normally states are added to the $stateProvider during the config phase. If you want to add states at runtime, you'll need to keep a reference to the $stateProvider around.

    This code is untested, but should do what you want. It creates a service called runtimeStates. You can inject it into runtime code and then add states.

    // config-time dependencies can be injected here at .provider() declaration
    myapp.provider('runtimeStates', function runtimeStates($stateProvider) {
      // runtime dependencies for the service can be injected here, at the provider.$get() function.
      this.$get = function($q, $timeout, $state) { // for example
        return { 
          addState: function(name, state) { 
            $stateProvider.state(name, state);
          }
        }
      }
    });
    

    I've implemented some stuff called Future States in UI-Router Extras that take care of some of the corner cases for you like mapping urls to states that don't exist yet. Future States also shows how you can lazy load the source code for runtime-states. Take a look at the source code to get a feel for what is involved.

    这篇关于角 - UI路由器 - 编程方式添加国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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