Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module.run() and $state.go() angularJS

Tags:

I might have some kind of missunderstanding. Im using angular ui router and i have the next issue:

I have the next State provider:

angular .module('MainApp') .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider ) {     $stateProvider         .state('register', {             url: "/",             abstact: true,             views: {                 "side-bar": {                     controller: "GetSideBarCtrl"                 },                 "page-body@register": {                     templateUrl: "/App/Views/WelcomePage.html"                 }             },         })         .state('register.Register', {             url: "Register",             views: {                 "page-body": {                     controller: "RegisterCtrl",                     templateUrl: "/App/Views/Register.html"                 }             },         })         .state('register.Login', {             url: "Login",             views: {                 "page-body": {                     controller: "LoginCtrl",                     templateUrl: "/App/Views/Login.html"                 }             },         })         //For registered and loged in users!     /* */         .state('main', {             url: "/:userId",            // abstact: true,             views: {                 "side-bar": {                     controller: "GetSideBarCtrl"                 },                 "page-body@main": {                   //  controller: "LoginCtrl",                     templateUrl: "/App/Views/MainPage.html"                 }             },         });     $urlRouterProvider.otherwise('/'); }]); 

And run function

angular .module('MainApp') .run(['$rootScope', '$state', '$cookieStore', 'principal', function ($rootScope, $state, $cookieStore, principal) {     var cookies = $cookieStore.get('user');     if (cookies) {         principal.Authenticate(cookies.split(' ')[0], cookies.split(' ')[1]);         $state.go('main', { userId: cookies.split(' ')[0] });     } else {         $state.go('register.Login');     } }]); 

Everything works fine, when cookies are present and authenication works without any problems but $state.go does nothing, i cant figure out why, can you please help and explain me...

like image 489
dulebov.artem Avatar asked Oct 14 '14 07:10

dulebov.artem


People also ask

What is state go in angular?

The $state.go is an AngularJS directive that tells the view to update its URL to reflect the current state of the application. This directive is used to change the URL without navigating away from the current page.

What is state in AngularJS?

The STATE in AngularJS is a mechanism that allows us to update the view based on changes to the model. It is a two-way binding between data and DOM elements. Moreover, the State helps us keep track of data that changes over time, such as whether a particular button has been pressed or not.

What is Controlleras in AngularJS?

In AngularJS, a Controller is defined by a JavaScript constructor function that is used to augment the AngularJS Scope. Controllers can be attached to the DOM in different ways.


1 Answers

Yep, as suggested it looks like $state.go() doesn't work correctly in module.run().

I found that putting the $state code in a $timeout works, e.g.

angular.module('MainApp').run($timeout, $state, ...) {      $timeout(function() {         $state.go('main');     });  } 
like image 112
Dunc Avatar answered Sep 21 '22 18:09

Dunc