Skip to content

Controllers from Components attached to states not always activated in the same order #3168

@cadilhac

Description

@cadilhac

Using 1beta3

Because of a strange issue in my app, I realized something related to ui-router. I have a controller that broadcasts a message. This message is listened to in another controller. Sometimes, the message is caught, sometimes not. So I put some console.log in each $onInit of the controllers and they are anot always displayed in the same time. Obviously, in a particular order, the message is caught, in the other order, it isn't (since the listener did not register before the message was posted).

I understand that the context is vague but I will try to describe how the states are defined:

platform
|-- explore
| |-- userMarkerSelected

Here is the platform state:

        var platformState = {
            name: 'platform',
            redirectTo: 'explore',
            url: '/',
            views: {
                '': {
                    component: 'platform'
                },
                'main-map@platform': {
                    component: 'mainmap'   // This is the controller that listens to a message in $onInit
                }
            }
        }

Note that the main-map ui-view is no in a conditional node with ng-if or ng-show.
The userMarkerSelected state:

        var userMarkerSelectedState = {
            name: 'userMarkerSelected',
            parent: 'explore',
            url: '/user/:userid',
            views: {
                '': {
                    template: '<ui-view />'
                },
                'panel-content@platform': {
                    component: 'selecteduser'   // This is the controller that sends the message
                }
            }
        };

Since userMarkerSelected is a descendant state of platform, I expect that a view defined in the parent controller would be built before the view defined in a descendant. And as I told you sometimes yes (actually most of the time), but sometimes no.

So, if I console.log at the beginning of the controllers, this gives when it works:

Platform
Explore
MainMap
SelectedUser

and when this does not work:

Platform
Explore
SelectedUser
MainMap

Not sure what additional information I can give you.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions