AngularJS-如何从控制器内部更改状态 [英] AngularJS - How do I change the State from Inside the Controller

查看:187
本文介绍了AngularJS-如何从控制器内部更改状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AngularJS的新手,想知道如何从Controller内部更改状态.

I am new to AngularJS and would like to know how I change the State from within the Controller.

例如,我通常在单击按钮时更改状态:

For example, I typically change the state on a button click:

<input type="submit" class="btn btn-lg btn-primary btn-block" value="Log In" ui-sref="main.navigation"/>

因此,在提交"按钮上,我的页面将更改为导航页面.这可以正常工作,但是如果我想先在控制器中执行一些逻辑,然后根据结果进行操作,我想更改为特定页面该怎么办.我该如何从控制器内部执行此操作,而不是通过单击按钮进行操作.

So on Submit of the button, my page will change to the navigation page. This works fine, but what if I want to do some logic in my controller first, and then based off the results, I would want to change to a specific page. How do I do this from within the controller, versus from a button click.

这是我的modules.js,以防您好奇:

Here is my modules.js in case you are curious:

angular.module('ecsMain', [
    'ui.router',
    'ui.bootstrap',
    'ngTable'
])
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('main/login');

    $stateProvider
        .state('main', {
            abstract: true,
            url: '',
            templateUrl: 'view/main.html'
        })
        .state('main.login', {
            url: '',
            controller: ECSMainController,
            templateUrl: 'view/login.html'
        })
        .state('main.phoneCalls', {
            url: '',
            controller: AccordionDemoCtrl,
            templateUrl: 'view/phoneCalls.html'
        })
        .state('main.navigation', {
            url: '',
            controller: ModalDemoCtrl,
            templateUrl: 'view/navigation.html'
        })
        .state('main.myModalContent', {
            url: '',
            controller: ModalDemoCtrl,
            templateUrl: 'view/myModalContent.html'
        })
        .state('main.alertMessage', {
            url: '',
            controller: ModalDemoCtrl,
            templateUrl: 'view/alertMessage.html'
        })
}]);

谢谢

推荐答案

$state服务注入到您的控制器中,然后在您的控制器中...

inject $state service to your controller, then in your controller...

CONTROLLER

$scope.changeState = function () {
    $state.go('where.ever.you.want.to.go', {stateParamKey: exampleParam});
};

并将ng-click添加到您的按钮

HTML

<button ng-click="changeState()">Change State</button>

这篇关于AngularJS-如何从控制器内部更改状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆