简体   繁体   中英

route from parent to child triger resolve on parent

I have routes like this (using ui-router):

    $stateProvider
        .state('studies', {
            url: '/studies?all&field&term&page&sort&sort_dir',
            templateUrl: 'scripts/studies/studies.template.html',
            controller: 'StudiesController',
            onExit: function exitStudies(filterQuery) {
                filterQuery.emptyQuery();
            },
            menuElement: 'studies',
            resolve: {
                attributes: function (manageAttributes, attributeDefinition, $q) {
                    console.log('resolve');
                    var promises = {
                        columnDefinitions: attributeDefinition.fillColumnsDefinitions(),
                        userColumns: manageAttributes.queryAttributes()
                    }
                    return $q.all(promises)
                        .then(function (response) {
                        var attributes = _.map(response.userColumns, function (object) {
                            return _.find(manageAttributes.getAllAttributes(), function (attribute) {
                                return attribute.name === object.name;
                            });
                        });
                        manageAttributes.setAttributes(attributes);
                    });
                }
            }
        });
        ['favorites-list', 'favorites-filter', 'default-filter'].forEach(function(directive) {
            $stateProvider.state('studies.' + directive, {
                template: '<' + directive + '></' + directive + '>'
            });
        });

and when I navigate to child using:

favorites

the resolve console log is triggered, I don't have url in child routes because I don't want url to change when I navigate to child route.

When I added this code:

.run(function($rootScope, $state) {
    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
        console.log('state: ' + fromState.name + ' -> ' + toState.name);
    });
});

I've got this message:

resolve
state:  -> studies
resolve
state: studies -> studies.favorites-list

Why resolve is triggered when I navigate to child route?

Children inherit the resolves of their parents in ui-router. This is the way it's supposed to be. You could rearrange your code to make it not a child state. Or, possibly (since I've not tried this approach), override the resolve in the child state to return null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM