gpt4 book ai didi

javascript - 在 ui-router 中将参数传递给子状态时遇到问题

转载 作者:行者123 更新时间:2023-11-28 00:46:40 28 4
gpt4 key购买 nike

我有两个状态,一个是另一个的子状态。一个代表人员列表 (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();
}
}
});
};
})

最后,我的 Controller 中的函数转换为 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');
});
}]
});

关于javascript - 在 ui-router 中将参数传递给子状态时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27292750/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com