Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - How do I change the State from Inside the 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.

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'         }) }]); 

Thanks

like image 938
anad2312 Avatar asked Mar 21 '14 20:03

anad2312


People also ask

Can we have nested controller in AngularJS?

Nested Controllers: AngularJS allows using nested controllers. It means that you have specified a controller in an HTML element which is a child of another HTML element using another controller.

What is the responsibility of the controller in AngularJS?

The primary responsibility of the controller is to create a scope object and thereby pass it to the view. With this, we come to an end of this AngularJS Controllers article.

Which is the correct syntax of creating AngularJS controller?

AngularJS Example The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller. The myCtrl function is a JavaScript function. AngularJS will invoke the controller with a $scope object.

What is difference between controller and directive AngularJS?

A controller is usually used to contain and maintain the logic for your view, which gets bound to your view via $scope. A directive is something that you might use repeatedly and is called in your view directly through the directive name which you can pass in as an attribute.


1 Answers

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

CONTROLLER

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

and add ng-click to your button

HTML

<button ng-click="changeState()">Change State</button> 
like image 137
Poyraz Yilmaz Avatar answered Sep 19 '22 15:09

Poyraz Yilmaz