AngularJS - $routeProvider's resolve property results in undefined object in corresponding controller

51 views Asked by At

I have the below shown resolve property assigned to the "/users" route. The $rootScope.resource has several HATEOAS style linkrels in it; "users" being one of the rels... Ultimately, I'm trying to follow ($get) the "users" linkrel and assign the output (another level of HATEOAS rels) to the "usersResource" property, which I will then access through my controller:

.when("/users", {
            templateUrl: "/static/views/users.html",
            controller: "usersController",
            controllerAs: "usersController",
            resolve: {

                usersResource: function ($rootScope) {

                        if ($rootScope.resource) {

                        $rootScope.resource.$request().$get("users",{
                                linksAttribute: "_links",
                                embeddedAttribute: "_embedded"
                            })
                            .then(function successCallback(data) {
                                return data;

                            }, function errorCallback(data) {
                                return {}

                            })

                    }
                    else
                        return {};
                }

            }

        })

Now, when I'm trying to access the "usersResource" from my controller I get undefined. The controller has currently no logic. I'm merely trying to output the usersResource content into console for the time being...

(function () {

angular
    .module("HateoasApp")
    .controller("usersController", usersController);

usersController.$inject = ["usersResource"];

function usersController(usersResource) {
    var vm = this;

    // properties
    vm.usersResource = usersResource;

    // objects

    // functions
    console.log(usersResource);
}

})();

Any comments/suggestions will be greatly appreciated. Thanks!

1

There are 1 answers

0
JB Nizet On BEST ANSWER

You're not returning anything from your resolve function:

return $rootScope.resource.$request()...
^^^^^^