2

Referring to this question here, I am having the same problem.
How can we set up a multiple resolve, one with currentAuth and another with loadsequence which loads controllers and scripts?


Here is my state config :

.state('app.example', {
        url: "/example",
        templateUrl: "assets/views/example.html",
        resolve:{
            loadSequence: loadSequence('jquery-sparkline', 'exampleCtrl')
        },
        title: 'example',
        ncyBreadcrumb: {
            label: 'example'
        }
    })


and here is my loadsequece function:

function loadSequence() {
    var _args = arguments;
    return {
        deps: ['$ocLazyLoad', '$q',
        function ($ocLL, $q) {
            var promise = $q.when(1);
            for (var i = 0, len = _args.length; i < len; i++) {
                promise = promiseThen(_args[i]);
            }
            return promise;

            function promiseThen(_arg) {
                if (typeof _arg == 'function')
                    return promise.then(_arg);
                else
                    return promise.then(function () {
                        var nowLoad = requiredData(_arg);
                        if (!nowLoad)
                            return $.error('Route resolve: Bad resource name [' + _arg + ']');
                        return $ocLL.load(nowLoad);
                    });
            }

            function requiredData(name) {
                if (jsRequires.modules)
                    for (var m in jsRequires.modules)
                        if (jsRequires.modules[m].name && jsRequires.modules[m].name === name)
                            return jsRequires.modules[m];
                return jsRequires.scripts && jsRequires.scripts[name];
            }
        }]
    };
}


and here is my currentAuth factory:

currentAuth: ['Auth', function(Auth) {
                return Auth.$requireSignIn()
            }]

1 Answer 1

1

As described in the documentation of ui-router :

The resolve property is a map object. The map object contains key/value pairs of:

key – {string}: a name of a dependency to be injected into the controller.

factory - {string|function}: If string, then it is an alias for a service. Otherwise if function, then it is injected and the return value is treated as the dependency. If the result is a promise, it is resolved before the controller is instantiated and its value is injected into the controller.

so you can configure your state adding functions in your state resolve :

.state('app.example', {
    url: "/example",
    templateUrl: "assets/views/example.html",
     resolve: { 
scripts: loadSequence('jquery-sparkline', 'exampleCtrl').deps,
currentAuth: function(Auth){ return Auth.$requireSignIn();}
},
    title: 'example',
    ncyBreadcrumb: {
        label: 'example'
    }
})
Sign up to request clarification or add additional context in comments.

3 Comments

This works, but I'm still unable to access currentAuth from the controller. What am I missing?
@IvanCarosati Check your controller definition, you need to add currentAuth as argument of controller function. controller: function($scope, currentAuth){$scope.auth = currentAuth;}
I turned out that the controller has to be declared within the routes. I was using ng-controller and that execute the resolves but doesn't inject the resolved dependencies! Hope it help others landing on this page!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.