【问题标题】:Trouble passing parameter to child state in ui-router在 ui-router 中将参数传递给子状态时遇到问题
【发布时间】:2014-12-04 11:20:21
【问题描述】:

我有两种状态,一种是另一种的孩子。一个代表人员列表 (people),另一个代表当您单击个人以查看更多详细信息时 (people.detail)。

我的第一个状态按预期工作,并且有几个参数代表您可以应用的所有各种服务器端过滤器和分页。子状态是一个模态窗口,它按预期弹出,但我唯一的参数personID 从未进入$stateParams。不知道是不是要结合RESTful风格的URL和查询字符串的风格?

值得注意的是,$stateParams 填充了您对父状态的期望。

编辑:Plunker 表明我的意思 - http://plnkr.co/edit/eNMIEt?p=info(注意 ID 未定义)

app.js

$urlRouterProvider.otherwise('/people');

    $stateProvider
        .state('people', {
            url: '/people?pageNumber&pageSize&sortField&sortDirection&search&countryID&jobFunctionIDs&firmTypeIDs',
            templateUrl: 'Static/js/angular/views/people-list.html',
            controller: 'PeopleListController',
            resolve: {
                api: "api",
                people: function (api, $stateParams) {
                    //Code ommitted
                },
                countries: function (api) {
                    //Code ommitted
                },
                jobFunctions: function (api) {
                   //Code ommitted
                },
                firmTypes: function (api) {
                    //Code ommitted
                }
            }

        });

    modalStateProvider.state('people.detail', {
        url: "/{personID}",
        templateUrl: 'Static/js/angular/views/people-detail.html',
        controller: function () {

        },
        resolve: {
            person: function (api, $stateParams) {
                return api.people.getDetail($stateParams.personID);
            }
        }
    });

modalStateProvider 看起来像:

angular.module('myApp')
.provider('modalState', function ($stateProvider) {
    var provider = this;
    this.$get = function () {
        return provider;
    }
    this.state = function (stateName, options) {
        var modalInstance;
        $stateProvider.state(stateName, {
            url: options.url,
            onEnter: function ($modal, $state) {
                modalInstance = $modal.open(options);
                modalInstance.result['finally'](function () {
                    modalInstance = null;
                    if ($state.$current.name === stateName) {
                        $state.go('^');
                    }
                });
            },
            onExit: function () {
                if (modalInstance) {
                    modalInstance.close();
                }
            }
        });
    };
})

最后我在控制器中的函数转换到people.detail 状态:

    $scope.transitionToPersonDetail = function (personID) {
        $state.transitionTo('.detail', { personID: personID }, { location: true, inherit: true, relative: $state.$current, notify: false });
    };

【问题讨论】:

    标签: javascript angularjs angular-ui-router


    【解决方案1】:

    经过更多检查后,我仍然不完全确定为什么会发生这种情况,我认为这与 modalStateProvider$stateParams 的范围有关,并且状态还没有“准备好” ”。然而,所有这些纯属猜测。

    我用这段代码修复了它:

        $stateProvider.state('people.detail', {
            url: '/{personID:int}',
            onEnter: ['$stateParams', '$state', '$modal', 'api', function($stateParams, $state, $modal, api) {
                $modal.open({
                    templateUrl: 'Static/js/angular/views/people-detail.html',
                    controller: function(person) {
                        console.log(person);
                    },
                    resolve: {
                        person: function() {
                            return api.people.getDetail($stateParams.personID);
                        }
                    }
                }).result['finally'](function(result) {
                    $state.transitionTo('people');
                });
            }]
        });
    

    【讨论】:

      【解决方案2】:

      据我所知,'resolve' 的值可直接在控制器中使用,尽管我认为值得检查是否触发了子视图的控制器

      modalStateProvider.state('people.detail', {
          url: "/:personID",
          templateUrl: 'Static/js/angular/views/people-detail.html',
          controller: function () {
            console.log(person)
          },
          resolve: {
              person: function (api, $stateParams) {
                  return api.people.getDetail($stateParams.personID);
              }
          }
      });
      

      【讨论】:

      • 控制器肯定被触发了。如果我删除了 api.people.getDetail($stateParams.personID) 调用(由于 personID 未定义,它从我的 Web 服务返回 500)并替换为返回文字,那么它就会被记录下来。此处显示 - i.imgur.com/pHwe0lo.png
      • url: "/:personID", 你试过 ui-router 参数模式吗? ;)
      • 不幸的是,':personID 语法可以与大括号语法互换,根据文档 - github.com/angular-ui/ui-router/wiki/URL-Routing
      • 我的想法已经用完了,如果没有“工作”示例,我将无能为力
      • 创建了一个 plunker 来显示我所看到的,你应该希望看到处于状态 people.detail 的 personID 未定义 - plnkr.co/edit/eNMIEt?p=info
      猜你喜欢
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多