我设置了以下状态:
var questions = {
name: 'questions',
url: '/questions',
views: {
'menu': {
templateUrl: function (stateParams) {
return '/Content/app/questions/partials/menu.html'
},
controller: 'QuestionsMenuController'
},
}
}
var questionsContent = {
name: 'questions.content',
parent: 'questions',
url: '/:content',
views: {
'content@': {
templateUrl: function (stateParams) {
var isNumber = !isNaN(parseFloat(stateParams.content));
return isNumber ? '/Content/app/questions/partials/detail.html' :
'/Content/app/questions/partials/content.html'
},
controller: 'QuestionsContentController'
},
}
}
和:
$stateProvider
.state(questions)
.state(questionsContent);
当我使用 /questions 进入菜单时,控制器会获取问题列表并填充 $scope.questionHeaders 对象。
var url = '/api/Question/GetQuestionHeaders?id=0';
$http.get(url)
.success(function (data, status, headers, config) {
$scope.questionHeaders = data;
$scope.currentQuestion = $scope.questionHeaders[0].questionId;
$state.transitionTo('questions.content', { content: $scope.currentQuestion })
})
.error(function (data, status, headers, config) {
alert("Error: No data returned from " + url);
});
在此之后,我想转换到列表中的第一个,所以我编写了代码:
$state.transitionTo('questions.content', { content: $scope.currentQuestion })
但是,当我跟踪它时,它只会停留在无限循环中,并且不会转到新的 /questions/5 (当 5 是 questionHeaders[0].questionId 时)。
有人可以给我一些建议,告诉我如何让它过渡到 /questions/5 吗?如果它进入新的浏览器href,我很高兴,但我需要直接编码(如何)还是ui-router可以为我做这个?