我有两种状态,一种是另一种的状态。其中一个代表人物列表(people
),另一个代表当您单击某个人时查看更多详细信息(people.detail
)。在ui-router中传递参数给子状态的麻烦
我的第一个状态按预期工作,并有几个参数代表您可以应用的所有各种服务器端过滤器和分页。孩子状态是一个模式窗口,弹出如预期,但我唯一的参数personID
从来没有把它变成$stateParams
。我想知道是否可以组合REST风格的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 });
};
控制器绝对触发。如果我删除'api.people.getDetail($ stateParams.personID)'调用(它将从web服务返回一个500,因为personID未定义),并替换为返回一个文字,然后将其记录下来。此处显示 - http://i.imgur.com/pHwe0lo.png – sxthomson 2014-12-04 11:35:16
'url:“/:personID”,'您是否尝试过ui-router params模式? ;) – maurycy 2014-12-04 11:40:07
不幸的是,':personID'语法与根据文档的大括号语法是可以互换的 - https://github.com/angular-ui/ui-router/wiki/URL-Routing – sxthomson 2014-12-04 11:45:59