# Issues w/ routing / $state.go

**URL:** <https://forum.ionicframework.com/t/issues-w-routing-state-go/9418>\
**Category:** ionic-v1\
**Created:** [September 10, 2014, 1:05pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418 "2014-09-10T13:05:55Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![arthoc](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@arthoc](https://forum.ionicframework.com/u/arthoc)\
**Post date:** [September 10, 2014, 1:05pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/1 "2014-09-10T13:05:55Z")

</div>

Hey there,  
I’ve an issue w/ $state.go() to change views.

I’ve implemented a login-system which broadcasts an “AuthEvent” once the user is logged in (or if there’s a login-token available).  
Unfortunately, my $state.go()-directives don’t do anything expect for changing the URL (i.e. index.html#/ --\> index.html#/login) - but not the view on the screen! If I reload the page after the URL-change, everything is displayed correctly.

My routing:

```
    .config(function($stateProvider, $urlRouterProvider) {
    $stateProvider.state('login', {
        url: '/login',
        views: {
            'login': {
                templateUrl: 'templates/login.html'
            }
        },
        controller: 'introCtrl'
    });
    $stateProvider.state('main', {
        url: '/',
        views: {
            'main': {
                templateUrl: 'templates/main.html'
            }
        },
        controller: 'mainCtrl'
    });

```

The routung-calls look like this:

```
$state.go('login');

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![dangeruponu](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@dangeruponu](https://forum.ionicframework.com/u/dangeruponu)\
**Post date:** [September 10, 2014, 1:40pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/2 "2014-09-10T13:40:30Z")

</div>

When you catch the event, try wrapping the `$state.go('login')` in a `$timeout`. Also, if you are already on the login state/url then you will have to explicitly refresh. Do you have multiple directives trying to change the ui-state at once?

---

<div class="post-metadata">

**Author:** ![arthoc](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@arthoc](https://forum.ionicframework.com/u/arthoc)\
**Post date:** [September 10, 2014, 2:03pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/3 "2014-09-10T14:03:27Z")

</div>

Thanks for your reply! I tried wrapping my $state.go()-call in a $timeout, as suggested, even though I’m not sure what the purpose of that would be…?

```
function() {
  console.log('DEBUG: $state.go');
  $state.go('login');
},2000

```

This now takes 2s to change the URL (as expected), but the view doesn’t load anyways (at least until I refresh the page of course).  
I’ve also added a debug-log in front of every $state.go()-directive to find out if there are multiple calls occurring at once, but apparently, there’s only the one now wrapped inside the timeout.

Second question, hope it’s okay to put this in this thread as well: Is there any method to automatically fire a function ($http-event in my case) when a specific view is rendered? e.g., after the login, fetch data from the server and populate the page.

---

<div class="post-metadata">

**Author:** ![dangeruponu](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@dangeruponu](https://forum.ionicframework.com/u/dangeruponu)\
**Post date:** [September 10, 2014, 2:12pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/4 "2014-09-10T14:12:33Z")

</div>

The purpose of the `$timeout` is to add your ‘action’ to the end of the digest cycle. It’s an AngularJS thing. You don’t need to add a delay though, you would want to do something like this probably:

```
$timeout(function() {
  console.log('DEBUG: $state.go');
  $state.go('login');
});

```

To answer your second question, look at the events on the very bottom of this page: [https://github.com/angular-ui/ui-router/wiki](https://github.com/angular-ui/ui-router/wiki)

ionic uses ui-router underneath so these events will be fired accordingly.

---

<div class="post-metadata">

**Author:** ![Russ\_T](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/russ_t/32/74203_2.png) [@Russ\_T](https://forum.ionicframework.com/u/Russ_T)\
**Post date:** [January 6, 2015, 3:03am UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/5 "2015-01-06T03:03:51Z")

</div>

@arthoc did you manage to get this working please? I have

```
 .controller('PlayCtrl', function ($scope) {
    $scope.home = function () {
        $state.go('home');
    };
});

```

Which like you changes the url (when using ionic serve) but doesn’t update the view. I have tried

```
      $scope.home = function () {
            $timeout(function () {
            $state.go('home');
        });
    };

```

All I’m trying to do is get a to change state for me

```
<button class="button icon ion-arrow-return-left actionButton" ng-click="home()"></button>

```

Thank you

---

<div class="post-metadata">

**Author:** ![sreekanth](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sreekanth/32/67832_2.png) [@sreekanth](https://forum.ionicframework.com/u/sreekanth)\
**Post date:** [January 6, 2015, 3:22am UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/6 "2015-01-06T03:22:31Z")

</div>

@Russ_T can you copy-paste your html templates here?

I had a similar issue with changing routes/states, but it turned out to be html templates being incompatible with the Ionic version that i was using.

```
               <ion-side-menu side="left" width="200">
                    <header class="bar bar-header bar-stable">
                        <h1 class="title">Left</h1>
                    </header>
                    <ion-content class="has-header">
                        <ion-list>
                            <ion-item nav-clear menu-close href="#/event/check-in">
                                Check-in
                            </ion-item>
                            <ion-item nav-clear menu-close href="#/event/attendees">
                                Attendees
                            </ion-item>
                        </ion-list>
                    </ion-content>
                </ion-side-menu>

```

But after changing it to the following, routes worked fine.

```
               <ion-side-menu side="left" width="200">
                    <!-- changed header to ion-header-bar -->
                    <ion-header-bar class="bar bar-header bar-stable">
                        <h1 class="title">Left</h1>
                    </ion-header-bar>
                    <ion-content>
                        <!-- replaced ion-list and ion-item below -->
                        <ul class="list">
                            <a class="item" menu-close href="#/event/check-in">
                                Check-in
                            </a>
                            <a class="item" menu-close href="#/event/attendees">
                                Attendees
                            </a>
                        </ul>
                    </ion-content>
                </ion-side-menu>

```

It could be something related to your html templates/partials too. Check it out…

---

<div class="post-metadata">

**Author:** ![cyprusglobe](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyprusglobe/32/64855_2.png) [@cyprusglobe](https://forum.ionicframework.com/u/cyprusglobe)\
**Post date:** [January 6, 2015, 3:37am UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/7 "2015-01-06T03:37:36Z")

</div>

Instead of href u could do `ui-sref`

> **[angular-ui/ui-router](https://github.com/angular-ui/ui-router/wiki/Quick-Reference#ui-sref)**
>
> ui-router - The de-facto solution to flexible routing with nested views in AngularJS

---

<div class="post-metadata">

**Author:** ![Russ\_T](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/russ_t/32/74203_2.png) [@Russ\_T](https://forum.ionicframework.com/u/Russ_T)\
**Post date:** [January 6, 2015, 4:09am UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/8 "2015-01-06T04:09:25Z")

</div>

Hi @sreekanth, thanks for your help.

I have tried to change my templates to match yours, but it hasn’t changed anything. I have changed around a default tab project, on one of the tabs I would like to go to an entirely different state outside of the tabs.

Index.html

```
    <ion-nav-bar align-title="left">
    <ion-nav-back-button></ion-nav-back-button>
</ion-nav-bar>

<ion-nav-view name="index" class="paddedBottom">
</ion-nav-view>

```

tabs.html

```
<ion-view title="tabs" view-title="My app name">
<ion-nav-title>
    <img src="img/logo.png" height="40px">
    My app name
</ion-nav-title>
</ion-view>

<ion-tabs class="tabs-striped tabs-color-light">   
<ion-tab title="tab1" icon-off="ion-ios-moon-outline" icon-on="ion-ios7-moon" ui-sref="home.tab1">
    <ion-nav-view name="tab-experience"></ion-nav-view>
</ion-tab>
<ion-tab title="tab2" icon-off="ion-ios7-play-outline" icon-on="ion-ios7-play" ui-sref="home.tab2">
    <ion-nav-view name="tab-play"></ion-nav-view>
</ion-tab>
</ion-tabs>

```

tab2.html

```
<div></div>
<ion-content>
<ul class="list">
    <a class="item item-avatar item-icon-right" ng-repeat="bla in blas | orderBy : 'favourite' : true" type="item-text-wrap" href="#/tab/settings/{{experience.id}}">
        <img ng-src="{{bla.icon}}">
        <h2>{{bla.name}}</h2>
    </a>
</ul>
</ion-content>

<button class="button icon ion-play actionButton" ng-click="newState()"></button>

```

newState.html

```
<div class="fade2black"></div>

<button class="button icon ion-arrow-return-left actionButton" ng-click="backToTabs()"></button>

```

I’m not sure that’ll shed any light, but it’s most odd. My backToTabs() and newState() are being called ok as I’ve also put an alert in with them.

Thanks again 😄

---

<div class="post-metadata">

**Author:** ![sreekanth](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sreekanth/32/67832_2.png) [@sreekanth](https://forum.ionicframework.com/u/sreekanth)\
**Post date:** [January 6, 2015, 4:26am UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/9 "2015-01-06T04:26:12Z")

</div>

@Russ_T Could you use [plnkr.co](http://plnkr.co) and paste your entire code there? I could debug there…

---

<div class="post-metadata">

**Author:** ![Russ\_T](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/russ_t/32/74203_2.png) [@Russ\_T](https://forum.ionicframework.com/u/Russ_T)\
**Post date:** [January 6, 2015, 5:58pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/10 "2015-01-06T17:58:36Z")

</div>

@sreekanth, thank you for your help.

I have added it to [http://plnkr.co/edit/uKNzS0UJOhYw2fjpRZVy?p=preview](http://plnkr.co/edit/uKNzS0UJOhYw2fjpRZVy?p=preview), but I cannot at the moment get it to work. I will keep fiddling until I have figured plunker out though 😄

---

<div class="post-metadata">

**Author:** ![sreekanth](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sreekanth/32/67832_2.png) [@sreekanth](https://forum.ionicframework.com/u/sreekanth)\
**Post date:** [January 6, 2015, 8:16pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/11 "2015-01-06T20:16:09Z")

</div>

@Russ_T There were some typos and missing imgs… I have commented out missing img tags and fixed the typos … check this out… [http://plnkr.co/edit/QOhlTRSxsHNW640uKkKU?p=preview](http://plnkr.co/edit/QOhlTRSxsHNW640uKkKU?p=preview)

---

<div class="post-metadata">

**Author:** ![Russ\_T](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/russ_t/32/74203_2.png) [@Russ\_T](https://forum.ionicframework.com/u/Russ_T)\
**Post date:** [January 6, 2015, 9:31pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/12 "2015-01-06T21:31:13Z")

</div>

It works! Hoorah!! This will teach me to code at 4am, but I couldn’t help myself I was getting so close to getting it working.

Thank you very much for your help, I’ll go off and digest what I did wrong 😊

---

<div class="post-metadata">

**Author:** ![acveer](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@acveer](https://forum.ionicframework.com/u/acveer)\
**Post date:** [January 16, 2015, 12:30am UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/13 "2015-01-16T00:30:17Z")

</div>

Experiencing lazy-loading/Cache issue in `"version": "1.0.0-beta.14",`  
Where the controller is not being instantiated, when an state is being invoked from another controller like:

```
 $state.go('tab.home');

```

$state.go() worked fine & controller instantiated successfully in this `"version": "1.0.0-beta.8",`

$state is being injected into controller fine.  
tried below combinations (Sometimes with event.preventDefault();), but to no avail.

```
$state.go('tab.home', {reload: true});

$state.transientTo('tab.home');
```

---

<div class="post-metadata">

**Author:** ![acveer](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@acveer](https://forum.ionicframework.com/u/acveer)\
**Post date:** [January 16, 2015, 4:08am UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/14 "2015-01-16T04:08:33Z")

</div>

The issue I was facing above is due to the cached views introduced in _“version”: “1.0.0-beta.14”,_. At this time, I was able to resolve it by disabling cache for all views through config provider.

```
$ionicConfigProvider.views.maxCache(0);

```

It would be neat to have this option available/controllable for a view than for the entire app, something like:

```
$state.go('tab.home',{}, {reload:true});
```

---

<div class="post-metadata">

**Author:** ![sreekanth](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sreekanth/32/67832_2.png) [@sreekanth](https://forum.ionicframework.com/u/sreekanth)\
**Post date:** [January 16, 2015, 9:06am UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/15 "2015-01-16T09:06:01Z")

</div>

@acveer You could disable cache per view/state too (as shown below)… For more details, [http://ionicframework.com/docs/api/directive/ionNavView/](http://ionicframework.com/docs/api/directive/ionNavView/)

Disable cache within state provider

```
$stateProvider.state('myState', {
   cache: false,
   url : '/myUrl',
   templateUrl : 'my-template.html'
})

```

Disable cache with an attribute

```
<ion-view cache-view="false" view-title="My Title!">
  ...
</ion-view>
```

---

<div class="post-metadata">

**Author:** ![acveer](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@acveer](https://forum.ionicframework.com/u/acveer)\
**Post date:** [January 16, 2015, 4:00pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/16 "2015-01-16T16:00:48Z")

</div>

Thank you @sreekanth. Both solutions work great.

---

<div class="post-metadata">

**Author:** ![saamorim](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/saamorim/32/75635_2.png) [@saamorim](https://forum.ionicframework.com/u/saamorim)\
**Post date:** [March 20, 2015, 2:12pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/17 "2015-03-20T14:12:59Z")

</div>

Does anyone think this is promotable to a bug? The fact that disabling the cache fixes this “simply navigation” workflow makes me think what are the real usages of cache system and what are the risks of it.

Regards

---

<div class="post-metadata">

**Author:** ![nnduc1994](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nnduc1994](https://forum.ionicframework.com/u/nnduc1994)\
**Post date:** [May 22, 2015, 10:53pm UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/18 "2015-05-22T22:53:19Z")

</div>

Have you find a solution? $state.go still doesn’t work even though I have disable the cache

---

<div class="post-metadata">

**Author:** ![danielcourtness](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@danielcourtness](https://forum.ionicframework.com/u/danielcourtness)\
**Post date:** [September 28, 2015, 7:44am UTC](https://forum.ionicframework.com/t/issues-w-routing-state-go/9418/19 "2015-09-28T07:44:30Z")

</div>

> [@dangeruponu](#):
>
> When you catch the event, try wrapping the $state.go(‘login’) in a $timeout.

Using an empty $timeout fixed an issue I was having (on iOS), where the $state.go(state) call would sometimes ‘go’ to the very same view. Will keep DOM rendering behaviour in mind in future - thanks!
