I'm building an app with angular+ionic that uses a classic three-button menu at the bottom with three ion-tabs in it. When a user clicks a tab, that template opens through ui-router.
I have states like this:
$stateProvider
.state('other', {
url: "/other",
abstract: true,
templateUrl: "templates/other/other.html"
})
In the template I do something like:
<ion-nav-view name="other" ng-init="doSomething()"></ion-nav-view>
I'm aware that I can write the doSomething() function in my controller and just call it manually there. That gives me the same problem though. I can't seem to figure out how to call the doSomething() function more than once, whenever somebody opens that view.
Right now, the doSomething() function gets called just fine, but only the first time that view/tab gets opened by the user. I'd like to call a function (to update geolocation) whenever a user opens that view or tab.
What would be a correct way to implement that?
Thanks for helping out !
Considering Ionic's ability to cache view elements and scope data mentioned above, this might be another way of doing, if you want to run the controller every time the view gets loaded. You can globally disable the caching mechanism used by ionic by doing:
$ionicConfigProvider.views.maxCache(0);
Else, the way I had it working for me was doing
This is to clear the cache before leaving the view to re-run controller every time you enter back again.
If you have assigned a certain controller to your view, then your controller will be invoked every time your view loads. In that case, you can execute some code in your controller as soon as it is invoked, for example this way:
And in your controller:
Edit: You might try to track state changes and then execute some code when the route is changed and a certain route is visited, for example:
You can find more details here: State Change Events.
I had a similar problem with ionic where I was trying to load the native camera as soon as I select the camera tab. I resolved the issue by setting the controller to the ion-view component for the camera tab (in tabs.html) and then calling the $scope method that loads my camera (addImage).
In www/templates/tabs.html
The addImage method, defined in AddMediaCtrl loads the native camera every time the user clicks the "Camera" tab. I did not have to change anything in the angular cache for this to work. I hope this helps.