在 ui 路由器中将参数传递到子状态时出现问题
Trouble passing parameter to child state in ui-router
我有两种状态,一种是另一种的子状态。一个代表人员列表(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);
}
}
});
相关文章:
- React.js状态问题
- 使用$.data()更改click事件的状态时出现问题
- Chrome动画CSS3三维立方体与悬停状态问题
- HTML5/jQuery:回推状态问题
- 角度 UI 状态转换问题
- 将状态设置为“输入名称”字段的值时遇到问题
- 在 JavaScript 函数中保持变量处于活动状态时出现问题
- 在 ui 路由器中将参数传递到子状态时出现问题
- j查询悬停问题 - 最后一个 li 元素保持悬停状态
- 重置状态导致 React mixin 出现问题
- 角度状态的问题和解决方案
- React.js中setInterval内的访问状态问题
- 问题显示twitter状态/user_timeline MVC3.Net
- 使用<脚本src=..查找站点的状态代码-安全问题
- 在维护自定义jQuery菜单上的状态时遇到问题
- Onkeyup或onkeypress javascript事件问题-密码状态更新
- Javascript的问题.无法更新状态
- ReactJS -检测状态变化的问题
- angular ui路由器状态问题
- Jquery切换状态问题