Henkie85
Henkie85

Reputation: 245

angularjs resolve promise before controllers starts

I am looking for a way to resolve my promise before the controller starts kicking in. But whatever I try, the controller is already running and the result is still waiting. Can anyone help me :(

templateUrl: 'tag/details.html',
resolve: {
    result: function (TagResource, $route) {
        TagResource.get({ id: $route.current.params.id }).$promise.then(function (data) {
            return data
        });
    }
}

Upvotes: 0

Views: 160

Answers (1)

georgeawg
georgeawg

Reputation: 48948

Return the promise:

templateUrl: 'tag/details.html',
resolve: {
    result: function (TagResource, $route) {
        ̶T̶a̶g̶R̶e̶s̶o̶u̶r̶c̶e̶.̶g̶e̶t̶(̶{̶ ̶i̶d̶:̶ ̶$̶r̶o̶u̶t̶e̶.̶c̶u̶r̶r̶e̶n̶t̶.̶p̶a̶r̶a̶m̶s̶.̶i̶d̶ ̶}̶)̶
        return TagResource.get({ id: $route.current.params.id })
        .$promise.then(function (data) {
            return data
        });
    }
}

BETTER YET

Skip the .then method:

templateUrl: 'tag/details.html',
resolve: {
    result: function (TagResource, $route) {
        ̶T̶a̶g̶R̶e̶s̶o̶u̶r̶c̶e̶.̶g̶e̶t̶(̶{̶ ̶i̶d̶:̶ ̶$̶r̶o̶u̶t̶e̶.̶c̶u̶r̶r̶e̶n̶t̶.̶p̶a̶r̶a̶m̶s̶.̶i̶d̶ ̶}̶)̶
        return TagResource.get({ id: $route.current.params.id }).$promise;
    }
}

Upvotes: 3

Related Questions