I am new to mobile application development with Ionic. On login and logout I need to reload the page, in order to refresh the data, however, $state.go('mainPage')
takes the user back to the view without reloading - the controller behind it is never invoked.
Is there a way to clear history and reload the state in Ionic?
None of the solutions mentioned above worked for a hostname that is different from
localhost
!I had to add
notify: false
to the list of options that I pass to$state.go
, to avoid calling Angular change listeners, before$window.location.reload
call gets called. Final code looks like:$state.go('home', {}, {reload: true, notify: false});
>>> EDIT - $timeout might be necessary depending on your browser >>>
$timeout(function () { $window.location.reload(true); }, 100);
<<< END OF EDIT <<<
More about this on ui-router reference.
The correct answer:
The controller is called only once, and you SHOULD preserve this logic model, what I usually do is:
I create a method
$scope.reload(params)
At the beginning of this method I call
$ionicLoading.show({template:..})
to show my custom spinnerWhen may reload process is finished, I can call
$ionicLoading.hide()
as a callbackFinally, Inside the button REFRESH, I add
ng-click = "reload(params)"
The only downside of this solution is that you lose the ionic navigation history system
Hope this helps!
I have found a solution which helped me to get it done. Setting
cache-view="false"
onion-view
tag resolved my problem.As pointed out by @ezain reload controllers only when its necessary. Another cleaner way of updating data when changing states rather than reloading the controller is using broadcast events and listening to such events in controllers that need to update data on views.
Example: in your login/logout functions you can do something like so:
Now in your mainPage controller trigger the changes in the view by using the $on function to listen to broadcast within the mainPage Controller like so:
Reload the page isn't the best approach.
you can handle state change events for reload data without reload the view itself.
read about ionicView life-cycle here:
http://blog.ionic.io/navigating-the-changes/
and handle the event beforeEnter for data reload.