Javascript 在ui路由器中将参数传递到子状态时出错
我有两个州,一个是另一个的孩子。一个表示人员列表(Javascript 在ui路由器中将参数传递到子状态时出错,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,我有两个州,一个是另一个的孩子。一个表示人员列表(人员),另一个表示当您单击个人以查看更多详细信息(人员.详细信息) 我的第一个状态按预期工作,并且有几个参数,这些参数表示您可以应用的所有不同的服务器端过滤器和分页。子状态是一个模式窗口,按预期弹出,但我唯一的paramaterpersonID从未进入$stateParams。我想知道这是否与RESTful样式URL和查询字符串样式的组合有关 也许值得注意的是,$stateParams填充了您期望从父状态获得的所有内容 编辑:Plunker显示我
人员
),另一个表示当您单击个人以查看更多详细信息(人员.详细信息
)
我的第一个状态按预期工作,并且有几个参数,这些参数表示您可以应用的所有不同的服务器端过滤器和分页。子状态是一个模式窗口,按预期弹出,但我唯一的paramaterpersonID
从未进入$stateParams
。我想知道这是否与RESTful样式URL和查询字符串样式的组合有关
也许值得注意的是,$stateParams
填充了您期望从父状态获得的所有内容
编辑:Plunker显示我的意思-(注意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();
}
}
});
};
})
最后,我在控制器中的功能转换为人物。详细信息状态:
$scope.transitionToPersonDetail = function (personID) {
$state.transitionTo('.detail', { personID: personID }, { location: true, inherit: true, relative: $state.$current, notify: false });
};
据我记忆所及,“resolve”的值在controller中直接可用,尽管我认为值得检查您的子视图的controller是否被触发
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);
}
}
});
经过更多的检查,我仍然不能完全确定为什么会发生这种情况,我认为这与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');
});
}]
});
控制器肯定被触发了。如果我删除api.people.getDetail($stateParams.personID)
调用(在personID未定义时从我的web服务返回一个500)并替换为返回一个文本,那么它将被记录。此处显示-url:“/:personID”,
您尝试过ui路由器参数模式吗?;)不幸的是,”:personID
语法可以与花括号语法互换,根据文档-我已经没有想法了,没有“工作”示例,我不能做更多了创建一个plunker来显示我看到的东西,希望您在状态people.detail
未定义时可以看到personID-