动态附加数据state.go功能和resolve属性检索 [英] Attach data dynamically to state.go function and retrieve in resolve property

查看:651
本文介绍了动态附加数据state.go功能和resolve属性检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父状态:

.state('schoolyears', {
                url: '/schoolyears',
                views: {
                    'menu@': {
                        template: '<h1>Start your schoolyear action...</h1>'
                    },
                    'content@': {
                        templateUrl: "../views/schoolyear/schoolyears.html",
                        resolve: {
                            schoolyears: function (schoolyearService) {
                                return schoolyearService.getSchoolyears();
                            }
                        },
                        controller: 'SchoolyearsController'
                    }
                }
            })
.state('schoolyears.selected', {
                url: '/:id'
            })

和一个孩子的状态:

 .state('schoolyears.selected.dates.day', {
                url: '/day/:year-:month-:day',
                views: {
                    'planner@schoolyears.selected.dates': {
                        templateUrl: '../views/lessonplanner/lessonplanner.day.html',
                        resolve: {
                            periods: function (periodService, $stateParams, $state) {
                                var schoolyearId = $stateParams.id;
                                var firstDayOfWeek = $state.current.data.firstDayOfWeek;
                                var periods = periodService.getPeriods(schoolyearId, firstDayOfWeek);
                                return periods;
                            }
                        },
                        controller: 'DateplannerDayController'
                    }
                }
            })

当我在SchoolyearController.js打开一个学年:

When I open a schoolyear in the SchoolyearController.js:

 $scope.open = function () {
        var entity = $scope.gridApi.selection.getSelectedRows()[0];
        var firstDayOfWeek = entity.firstDayOfWeek;
        $state.go('schoolyears.selected.dates.day', { id: $state.params.id}, {firstDayOfWeek: firstDayOfWeek}, {location: false, inherit: false});
    };

我想从选定项目学年通过Firstdayofweek可我想通过检索$ state.current.data.firstDayOfWeek的UI路由器的决心,这里面Firstdayofweek可值,但有一个像Firstdayofweek可没有这样的变量。

I want to pass the firstDayOfWeek from the selected schoolyear item and I want to retrieve this firstDayOfWeek value inside the resolve of the ui router via $state.current.data.firstDayOfWeek but there is no such variable like firstDayOfWeek.

我如何可以通过连接到state.go从UI路由器决心使其正常加载数据的数据?

How can I pass data attached to state.go and get it from the ui router resolve to load the data correctly?

推荐答案

由于这里记录:的解决,解析器的值可以是字符串或函数 - 在工厂

As documented here: Resolve, the value of resolver could be string or a function - the Factory.

这意味着,我们确实有机会获得DI我们需要的任何服务。而且,在angularjs世界,服务 - 作为一个单身,是最proprieate路怎么翻过共享应用程序设置。这将是解决办法,我建议去了。调整后的plunker,显示出这个想法可以找到 rel=\"nofollow\">。

That means, that we do have access to DI of any Service we need. And also, in angularjs world, Service - as a singleton, is the most proprieate way how to share settings accross the application. That would be solution I would suggest to go. The adjusted plunker, showing that idea could be found here.

我们将不得不推出一项服务,例如设置

We would have to introduce a service, e.g. Setting

.factory('Settings', function(){
  return {
    firstDayOfWeek : null,
  };
})

我们还需要该服务绑定到某些用户peferences:

We also have to bind that service to some user peferences:

.controller('SchoolyearsController', function($scope, Settings, schoolyears){
  $scope.Settings = Settings;
  ... 
  // bind the $scope.Settings.firstDayOfWeek to some selected value

和我们解析:

  periods: function (Settings, $stateParams) {
    var periods = {
      year: $stateParams.id,
      firstDayOfWeek : Settings.firstDayOfWeek
    };
    return periods;
  }

检查这里。也许示例实现更复杂一点,但我相信在这里,想法应该是清楚的。

Check that here. Maybe the example implementation is a bit more complex, but I believe that idea here should be clear

这篇关于动态附加数据state.go功能和resolve属性检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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