麻烦在ui-router中将参数传递给子状态

时间:2014-12-04 11:20:21

标签: javascript angularjs angular-ui-router

我有两个州,一个是另一个孩子。一个代表人员列表(people),一个代表点击个人查看更多详细信息(people.detail)。

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

值得注意的是,$stateParams中包含了您对父州的所有期望。

编辑:请说明我的意思 - 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 });
    };

2 个答案:

答案 0 :(得分:1)

经过更多的检查后,我仍然不完全确定为什么会发生这种情况,我认为这与modalStateProvider的{​​{1}}范围有关事实上,国家并没有准备好#34;所有这些都是纯粹的猜测。

我用这段代码修复了它:

$stateParams

答案 1 :(得分:0)

据我记得,来自'解决'可直接在控制器中使用,但我认为值得检查您的子视图的控制器是否被触发

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