I've got an issue with controller being called twice in an Ionic app
These are my states
$stateProvider.state('connected', {
abstract: true,
url: '/{deviceId}',
template: '<ion-nav-view/>'
}).state('connected.presets', {
abstract: true,
url: '/presets/{presetType}',
template: '<ion-nav-view/>'
}).state('connected.presets.index', {
cache: false,
url: '/',
templateUrl: 'views/presets/index.html',
controller: 'PresetsIndexController'
});
I try to navigate between states like this
$state.go('.', {presetType: 'default'});
// OR
$state.go('.', {presetType: 'custom'});
I can't find out why but the PresetsIndexController
is being called twice just by updating the presetType
param.
I've found someone that seem to have the same issue on this plunker http://plnkr.co/edit/znIa9XU9JsyUDjum2HHx?p=preview. To see the bug, open the javascript console and click on the reload icon, you should see hey
twice in the output.
I've also watched the $stateChangeStart
event but it seems to be fired once.
I've displayed the stack trace from the controller and the ion-nav-view
seems to be the root of the call. So from what I've tried, it seems that ion-nav-view
mess up with everything but no idea why nor how to solve this...
EDIT 1
If I move the presetType
param to the final state, the controller isn't called twice.
.state('connected.presets', {
abstract: true,
url: '/presets',
template: '<ion-nav-view/>'
}).state('connected.presets.index', {
cache: false,
url: '/{presetType}',
templateUrl: 'views/presets/index.html',
controller: 'PresetsIndexController'
})
But this is not a valid solution for me since I have other children of the connected.presets
state that I want to inherit of the presetType
.
EDIT 2
It seems that the problem comes from stateParams being changed on the parent state. My (shitty) workaround was to avoid changing stateParams on parent state...
I didn't want to update the ui-router lib since it's tied to my Ionic version. I think other devs had some trouble with ui-router too because Ionic dropped it in v2 and later.
I'm not happy with this workaround at all but hey, it's working.