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 !
Following up on the answer and link from AlexMart, something like this works:
By default, your controllers were cache and that is why your controller only fired once. To turn off caching for a certain controller you have to modify your
.config(..).state
and set thecache
option tofalse
. eg :for further reading please visit http://ionicframework.com/docs/api/directive/ionNavView/
I faced at the same problem, and here i leave the reason of this behavior for everyone else with the same issue.
To see full description and $ionicView events go to: http://ionicframework.com/blog/navigating-the-changes/
Why don't you disable the view cache with cache-view="false"?
In your view add this to the ion-nav-view like that:
Or in your stateProvider:
Either one will make your controller being called always.
This is probably what you were looking for:
Ionic caches your views and thus your controllers by default (max of 10) http://ionicframework.com/docs/api/directive/ionView/
There are events you can hook onto to let your controller do certain things based on those ionic events. see here for an example: http://ionicframework.com/blog/navigating-the-changes/
For example to @Michael Trouw,
inside your controller put this code. this will run everytime when this state is entered or active, you do not need to worry about disabling cache and it's a better approach.
You can have more examples of flexibility like $ionicView.beforeEnter -> which runs before a view is shown. And there are some more to it.