3

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.

15
  • are you using any directives/components in the same view that are using the same controller? Commented Apr 7, 2017 at 15:11
  • Hi thanks for your help. No the controller is unique to this view. I'm not calling with ng-controller by the way. Only in this exact state Commented Apr 7, 2017 at 15:18
  • I meant inside of a directive, I've seen a controller be called twice by requiring the controller into a directive. something-like-this-so-post Commented Apr 7, 2017 at 15:22
  • 2
    You have defined controller in ui-routing and you may have written ng-controller in your index.html. try removing ng-controller from index.html Commented Apr 7, 2017 at 15:32
  • 2
    @PeterPan666 according to this issue, it seems [email protected] or later has the fix. But as you know, ionic 1.3.1 has v0.2.13 so the only options you are left with is either update ionic version to have ui-router version containing fix or you find a workaround to achieve the same thing differently Commented Apr 7, 2017 at 18:19

0

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.