我如何让我的ui路由器从我的控制器进入到一个不同的状态?

时间:2022-08-19 18:53:10

I have the following states set up:

我建立了以下状态:

    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'
            },

        }
    }

and:

和:

    $stateProvider
        .state(questions)
        .state(questionsContent);

When I go my menu with /questions then the controller gets a list of questions and populates the $scope.questionHeaders object.

当我打开/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);
            });

Following this I want to transition to the first in the list so I coded:

接下来我想要过渡到列表中的第一个,所以我编码了:

$state.transitionTo('questions.content', { content: $scope.currentQuestion })

However when I trace this it just stays in an infinte loop and it does not go to the new /questions/5 (when 5 is the questionHeaders[0].questionId).

然而,当我跟踪它时,它只是停留在一个无限循环中,它不会转到新的/questions/5(当5是questionheader [0]. questionid)。

Can someone give me some advice as to how I can make it transition to /questions/5 ? I am happy if it goes to the new browser href but do I need to code that directly (how) or can ui-router do this for me?

谁能给我一些建议,让我把它过渡到/问题/5 ?如果它转到新的浏览器href,我很高兴,但是我需要直接编码(如何),或者ui-router能帮我这样做吗?

1 个解决方案

#1


34  

You can inject the $state service, and call it's transitionTo (go) method.

您可以注入$state服务,并调用它的transitionTo (go)方法。

.controller('SomeController', ['$state', function($state){
    $state.transitionTo('my.state', {arg:'arg'});
}]);

@Cody's question:

@Cody的问题:

You can also inject the $stateParams into your controller and go to a state with args:

您还可以将$stateParams注入控制器,并使用args进入状态:

.controller('SomeController', ['$state', '$stateParams', function($state, $stateParams){
        $state.go('my.state', {listId: $stateParams.listId, itemId: $stateParams.itemId});
    }]);

#1


34  

You can inject the $state service, and call it's transitionTo (go) method.

您可以注入$state服务,并调用它的transitionTo (go)方法。

.controller('SomeController', ['$state', function($state){
    $state.transitionTo('my.state', {arg:'arg'});
}]);

@Cody's question:

@Cody的问题:

You can also inject the $stateParams into your controller and go to a state with args:

您还可以将$stateParams注入控制器,并使用args进入状态:

.controller('SomeController', ['$state', '$stateParams', function($state, $stateParams){
        $state.go('my.state', {listId: $stateParams.listId, itemId: $stateParams.itemId});
    }]);