# $state.go() changing URL but not correctly loading template

**URL:** <https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618>\
**Category:** ionic-v1\
**Created:** [December 7, 2015, 1:48am UTC](https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618 "2015-12-07T01:48:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthonyi](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@anthonyi](https://forum.ionicframework.com/u/anthonyi)\
**Post date:** [December 7, 2015, 1:48am UTC](https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618/1 "2015-12-07T01:48:05Z")

</div>

The issue I am having is that I am trying to have a user login to my application and then once they have done so, redirect them to our home page. Up until recently this worked as expected, but now what happens is that the url will change but the template on the new page will not load correctly, showing only a blank screen.

If I use the chrome inspector (with Ionic serve) it shows that the elements have been loaded in the DOM and there doesn’t seem to be any CSS hiding it. Similarly, if I refresh the page (which goes directly to the home page as the user has already logged in) it works correctly.

**How I am changing state**  
`$ionicHistory.nextViewOptions({`  
`disableAnimate: true,`  
`disableBack: true });`$state.go('myapp.newsfeed)`

**My states**  
`$stateProvider`

```
.state('login', {
  url: "/login",
  templateUrl: "templates/login.html",
  controller: "logincontroller"
})

.state('myapp', {
  url: "/myapp",
  abstract: true,
  templateUrl: "templates/menu.html"
})

.state('myapp.newsfeed', {
  url: '/newsfeed',
  views: {
    'menuContent' :{
      templateUrl: "templates/newsfeed.html",
      controller: "globalfeedctrl"
    }
  }
})

```

**Code in globalfeedcontrol that I am sure is being called**  
`$scope.$on('$ionicView.enter', function() {`  
`console.log("We are entering the view");`

```
		if($rootScope.just_logged_in){
			$window.location.reload(true);

			$rootScope.just_logged_in = false;
			$scope.refreshContent();
			$rootScope.$broadcast('store-changed');
			$rootScope.$broadcast('reload-challenges');
			$rootScope.$broadcast('load-hub');
			$rootScope.$broadcast('reload-messages');
			$rootScope.$broadcast('reload-notifications');

			$rootScope.just_logged_in = false;
		}

		if(!initial_load){
			$scope.refreshContent();
		}
		initial_load = false;
		updateEngagement();
	})

```

I am sure that the controller code is being called both because the statement is logged but also because the network inspector shows that the necessary items are being loaded.

Also note that `$window.location.reload(true)` is a dirty little hack to get it to work.

Any help that could be provided would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![triniwiz](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/triniwiz/32/74300_2.png) [@triniwiz](https://forum.ionicframework.com/u/triniwiz)\
**Post date:** [December 7, 2015, 12:32pm UTC](https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618/2 "2015-12-07T12:32:34Z")

</div>

> [@anthonyi](#):
>
> $ionicHistory.nextViewOptions({disableAnimate: true,disableBack: true });$state.go('myapp.newsfeed)`

Should be

```
$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true });
$state.go('myapp.newsfeed')

```

---

<div class="post-metadata">

**Author:** ![anthonyi](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@anthonyi](https://forum.ionicframework.com/u/anthonyi)\
**Post date:** [December 7, 2015, 1:06pm UTC](https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618/3 "2015-12-07T13:06:38Z")

</div>

Sorry that was a typo ☹  
Is `$state.go('myapp.newsfeed')` in my file.

---

<div class="post-metadata">

**Author:** ![triniwiz](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/triniwiz/32/74300_2.png) [@triniwiz](https://forum.ionicframework.com/u/triniwiz)\
**Post date:** [December 7, 2015, 1:14pm UTC](https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618/4 "2015-12-07T13:14:51Z")

</div>

What does the login code look like if you don’t mind

---

<div class="post-metadata">

**Author:** ![anthonyi](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@anthonyi](https://forum.ionicframework.com/u/anthonyi)\
**Post date:** [December 7, 2015, 2:34pm UTC](https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618/5 "2015-12-07T14:34:39Z")

</div>

```
$scope.login = function(){

	if(!$scope.cred.username || !$scope.cred.password){
		showAlert("Username/Password cannot be blank");
		return;
	}

	Auth.login($scope.cred).then(function(user){

    if(!Auth.isAuthenticated()){
      showAlert("Your credentials were Incorrect");
      return;
    }

    console.log(user);

    window.localStorage.setItem("email", user.email);
    window.localStorage.setItem("token", user.authentication_token);
    window.localStorage.setItem("name", user.fullname);
    window.localStorage.setItem("userid", user.id);
    window.localStorage.setItem("created", user.created_at);
    window.localStorage.setItem("user_image", user.profile_image);
    window.localStorage.setItem("firstname", user.firstname);
    window.localStorage.setItem("lastname", user.lastname)

    $http.defaults.headers.common['X-User-Token'] = user.authentication_token;
    $http.defaults.headers.common['X-User-Email'] = user.email

  	$ionicHistory.nextViewOptions({
        disableAnimate: true,
        disableBack: true
    });

    $rootScope.just_logged_in = true;
    $state.go('myapp.newsfeed');
}, function(error, $rootScope){

	})
}

```

Sorry, indentation might be a bit funky.

---

<div class="post-metadata">

**Author:** ![triniwiz](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/triniwiz/32/74300_2.png) [@triniwiz](https://forum.ionicframework.com/u/triniwiz)\
**Post date:** [December 11, 2015, 5:57pm UTC](https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618/6 "2015-12-11T17:57:48Z")

</div>

Everything looks fine is there any link to the source ?

---

<div class="post-metadata">

**Author:** ![jinwang8942](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jinwang8942/32/94740_2.png) [@jinwang8942](https://forum.ionicframework.com/u/jinwang8942)\
**Post date:** [December 14, 2015, 3:18pm UTC](https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618/7 "2015-12-14T15:18:32Z")

</div>

Hello anthonyi  
Thank you for your great post.  
I have same issue. but … When i try this, it is works bad on my own.

## This is my LoginCtrl controller…
`
app.controller('LoginCtrl', function ($scope, $stateParams, $ionicHistory, $timeout, $state, ionicMaterialInk) {
$scope.doLogin = function() {
console.log(‘Doing login’, $scope.loginData);
```
// code if using a login system
$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true });
$state.go("app.home");

```
};
});
`

when i click login button. it is redirect to home … but UI (CSS) is all broken…  
When i refresh page, it is looks good.

What i am wrong? Can you help me?  
Thanks

---

<div class="post-metadata">

**Author:** ![triniwiz](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/triniwiz/32/74300_2.png) [@triniwiz](https://forum.ionicframework.com/u/triniwiz)\
**Post date:** [December 16, 2015, 12:48pm UTC](https://forum.ionicframework.com/t/state-go-changing-url-but-not-correctly-loading-template/38618/8 "2015-12-16T12:48:50Z")

</div>

```
   $scope.login = function(event){
event.preventDefault();
$state.go('app.home');
}

```

I was having a similar problem and that’s what i did
