1

I'm fairly new to angular, so sorry for the newbish question, but I'm totally stumped.

I am trying to preload my controller with data when the route changes using using resolve. I am getting data back from the server, and I'm injecting the resolve object into the controller, but something isn't working.

Here is the resolve portion of the ui-router

resolve: {
    categoryList: function ($http) {
        $http.get('cfc/category.cfc?method=getCategories')
            .success(function (data) {
                return data;
            });
    }
},...

and the controller

app.controller('CategoryController', ['$scope', '$http', 'categoryList', 
    function ($scope, $http, categoryList) {...

Could someone explain what I'm doing wrong?

2 Answers 2

3

Minor little detail:

resolve: {
    categoryList: function ($http, $q) {
        var defObj = $q.defer();
        $http.get('cfc/category.cfc?method=getCategories')
            .success(function (data) {
                defObj.resolve(data);
            });
        return defObj.promise;
    }
},...

What's happening is that your promise is resolving AFTER your controller has loaded, which means that your data was not passed in. By adding the deferred object and returning it, you get the promised data injected into your controller AFTER it resolves, and everything should be good. Also, you may want to handle the $http call failure with an error handler and insert some dummy data that you can detect and react to in your controller.

Sign up to request clarification or add additional context in comments.

Comments

0

Actually $http returns a promise by default so all you have to do is javascript return $http.get( ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.