Guo Star Guo Star - 1 year ago 93
AngularJS Question

how to get resolved data in $state object with ui-router 1.0

i am building an angular plugin named breadcrumb here, it works fine until i update ui-router version to '1.0.0-beta.2'. the cause is that i can't get resolved data from $state.$current.

in html



specify display name property to data.displayName in html

<breadcrumbs display-name-property="data.displayName"></breadcrumbs>


state config



displayName is dynamic

.state('app.dict.list.tree', {
...
data: {
displayName: '{{title}}'
},
resolve: {
title: ['$transition$', $transition$ => $transition$.params().title],
}
})


directive link function



i use $interpolate service to compile propertyReference(in this case '{{title}}') with the context $state.$current.locals.global.

var currentState = $state.$current;
interpolationContext = (typeof currentState.locals !== 'undefined') ? currentState.locals.globals : currentState;
displayName = $interpolate(propertyReference)(interpolationContext);


update ui-router to latest version, this code won't work anymore, currentState.locals is undefined, and i can't find a way to access resolved data in currentState object.

does anybody know how to do it? any help will be appreciated.

Answer Source
if(legacyRouter){
                    scope.$on('$stateChangeSuccess', function() {
                        updateBreadcrumbsArray();
                    });
                }else{
                    $injector.get('$transitions').onSuccess({}, function(trans){
                        resolved(trans, updateBreadcrumbsArray)
                    })
                }

                /**
                 * get all resolved data in state declare and use them to compile display name
                 * */
                function resolved(trans, fn){
                    var tokens = trans.getResolveTokens();
                    var resolves = {};
                    var promises = tokens.map(function(token){
                        var resolved = trans.injector().get(token);
                        resolves[token] = resolved;
                        return resolved;
                    });
                    $q.all(promises).then(function(){
                        fn(resolves);
                    })
                }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download