5

I have got a controller named newGroupCtrl whose definition is like :

.state('new_group', {
    url: '/new_group',
    templateUrl: 'templates/new_group.html',
    controller: 'newGroupCtrl'
})

.controller('newGroupCtrl', function ($scope, $rootScope,$ionicHistory,$window) {
    $rootScope.roomId = $scope.getRoom();

    $scope.getRoom = function () {
        var date = new Date;
        var minutes = date.getMinutes();
        var hour = date.getHours();
        return 'room_' + hour + '' + minutes;
    };
}

I reach this contoller from previous page by :

$window.location.href = ('#/new_group');

That's good until now. $rootScope.roomId variable is initialized in the newGroupCtrl controller properly.

From this new_group page, I navigate to another page. And when I navigate back to this page by calling $window.location.href = ('#/new_group');, $rootScope.roomId is not initialized again, instead its old value is still there. The state of the newGroupCtrl is preserved.

How can I completely reinitialize newGroupCtrl?

4
  • What does your getRoom() do? can you share its code details? Commented May 13, 2015 at 8:32
  • I added getRoom code as well Commented May 13, 2015 at 8:35
  • 1
    Since you're using ui-router, you should navigate with the $state service. $state.go('new_group'); Commented May 13, 2015 at 8:38
  • Actually i'm not having this problem in my app. angular-ui.github.io/ui-router/site/#/api/… Commented May 13, 2015 at 8:50

4 Answers 4

5

You need to tell state that reload controller each time when URL is getting accessed via browser by just adding adding reload option of state to true like reload: true.

Code

.state('new_group', {
    url: '/new_group',
    templateUrl: 'templates/new_group.html',
    controller: 'newGroupCtrl',
    reload: true //will reload controller when state is being access
});

You should use $state.go('new_group') instead of doing $window.location.href = ('#/new_group'); which will ensure that the route changes will recognize by ui-router.

Same SO answer here

Sign up to request clarification or add additional context in comments.

Comments

2

Since you are using Ionic Framework (Good Job), you can do this:

.controller('YourCtrl', function($ionicView){
  $ionicView.enter(function(){
    //code that you want to run, each time the view is active
  });
});

Comments

0

Remove the controller from :

.state('new_group', {
    url: '/new_group',
    templateUrl: 'templates/new_group.html',
})

And add the controller at "new_group.html" page with parent tag of the page like:

<div ng-controller="newGroupCtrl"></div>

1 Comment

this looks like hacky solution..:p
0

Also I found helpful (for Ionic Framework) to use

.state('new_group', {
    url: '/new_group',
    templateUrl: 'templates/new_group.html',
    cache: false
})

Reference to similar question and problems: Reloading current state - refresh data or in sjm's answer in Reinitialize Controller every time when visiting View - Ionic Framework

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.