角UI路由器:得到解决toState的$状态信息 [英] angular ui-router: get $state info of toState in resolve

查看:114
本文介绍了角UI路由器:得到解决toState的$状态信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这一点应在角UI的路由器尽可能 1.0.0alpha0 的。请参见发行说明 https://github.com/angular-ui /ui-router/releases/tag/1.0.0alpha0 和问题 https://github.com/angular-ui/ui-router/issues/1018 我创建的。

我想访问该州的名称和其他属性的应用程序被导航到使用上的决心时角UI的路由器。

原因是:我想要允许异步应用程序的输入页面之前加载某些用户数据(包括他们的访问权限)。

目前由于注射$状态进入决心指向状态你离开导航的形式,而不是你导航到一个,这是不可能的。

我知道我可以:


  • 获得使用toState其他与$ rootScope('$ stateChangeStart')的地方,并把它保存在例如我的设置服务。但我认为这是一个有些凌乱。

  • 硬code中的状态进入的决心,但我不希望重用我决心为所有页面

我还对UI路由器github上造成的问题(请+ 1,如果你有兴趣!):
https://github.com/angular-ui/ui-router/issues/1018

下面是我的code为止。任何帮助AP preciated!

 的.config(函数($ stateProvider){    $ stateProvider.state('SomePage的',{
        // ..
        解析:{
            用户数据:功能($ stateParams,$状态设置){
                返回Settings.getUserData()//加载用户数据不同步
                    。然后(功能(用户数据){
                        的console.log($ stateParams);
                        的console.log($状态);
                        //问题:$状态仍然指向你从导航离开状态
                    });
            }
        }
    });
});


解决方案

您可以随时装点 $状态接下来 toParams 属性:

  angular.config(函数($提供){
  $ provide.decorator('$状态',函数($代表,$ rootScope){
    $ rootScope。在$('$ stateChangeStart',函数(事件状态,则params){
      $ delegate.next =状态;
      $ delegate.toParams =参数;
    });
    返回$代表;
  });
});

和这样使用:

  .STATE('myState',{
    网址:'/事/ {ID}
    解析:{
        oneThing:函数($州){
            的console.log($ state.toParams,$ state.next);
        }
    }
});

Update: this should be possible in angular-ui-router as of 1.0.0alpha0. See the release notes https://github.com/angular-ui/ui-router/releases/tag/1.0.0alpha0 and the issue https://github.com/angular-ui/ui-router/issues/1018 I created.

I would like to access the state's name and other attributes the app is navigating to using angular ui-router when working on the resolve.

The reason: I want load some user data (including their access rights) asynchronously before allowing the app the enter that page.

Currently this is not possible because injecting $state into the resolve points to the state you're navigating away form, not to the one you're navigating to.

I know I can:

  • get the toState somewhere else with $rootScope('$stateChangeStart') and save it in my settings service for instance. But I think it's a little messy.
  • hard code the state into the resolve, but I don't want to reuse my resolve for all pages

I also created an issue on the ui-router github (Please + 1 if you are interested!): https://github.com/angular-ui/ui-router/issues/1018

Here's my code so far. Any help appreciated!

.config(function($stateProvider) {

    $stateProvider.state('somePage', {
        // ..
        resolve: {
            userData: function($stateParams, $state, Settings) {
                return Settings.getUserData() // load user data asynchronously
                    .then(function (userData) {
                        console.log($stateParams);
                        console.log($state);
                        // Problem: $state still points to the state you're navigating away from
                    });
            }
        }
    });
});

解决方案

You can always decorate $state with next and toParams properties:

angular.config(function($provide) {
  $provide.decorator('$state', function($delegate, $rootScope) {
    $rootScope.$on('$stateChangeStart', function(event, state, params) {
      $delegate.next = state;
      $delegate.toParams = params;
    });
    return $delegate;
  });
});

And use as such:

.state('myState', {
    url: '/something/{id}',
    resolve: {
        oneThing: function($state) {
            console.log($state.toParams, $state.next);
        }
    }
});

这篇关于角UI路由器:得到解决toState的$状态信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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