在 ui 路由器中将参数传递到子状态时出现问题

Trouble passing parameter to child state in ui-router

本文关键字:状态 问题 路由器 ui 参数传递      更新时间:2023-09-26

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

我的第一个状态按预期工作,并且有几个参数,这些参数表示您可以应用的所有各种服务器端筛选器和分页。 子状态是一个模态窗口,它按预期弹出,但我唯一的参数personID永远不会进入$stateParams。 我想知道是否与 RESTful 样式 URL 和查询字符串样式的组合有关?

也许值得注意的是,$stateParams填充了您期望从父状态获得的所有内容。

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

应用.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 });
    };

经过更多的检查,我仍然不完全确定为什么会发生这种情况,我认为这与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');
            });
        }]
    });

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

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);
        }
    }
});