2

I have a URL which gets hit and the model of the controller is data from an API call. At the moment by the time the template is rendered the data hasnt come back so I get a blank page.

I looked into and apprently Resolve is the approach to take but I've not had much luck. Below is what I have but I'm very new at Angular so it maybe completely the wrong approach.

//app.js    
(function () {
    'use strict';

    var app = angular.module('barbato', []).
           config(function ($routeProvider) {
               $routeProvider.
                   when('/', {
                       controller: 'ReposController',
                       templateUrl: '/Content/templates/repos.html',
                       resolve: {
                           myVar: function(repoService) {
                               return repoService.getItems();
                           }
                       }
                   }).
                   otherwise({ redirectTo: '/' });
           });

    app.factory('repoService', function ($http) {

        return {
            getItems: function () {
                $http.get('http://localhost:12008/getrepodata/jchannon').then(function (response) {
                    return response.data;
                });
            },
        };
    });

})();




//repo.js    
(function () {
    'use strict';

    var app = angular.module('barbato');

    var repooController = app.controller(
        'ReposController', ['$scope','myVar', function ($scope, myVar) {
            $scope.items = myVar;
        }
    ]);

})();
2
  • indeed resolve is the way to go. Take a look at this question Commented May 22, 2013 at 13:31
  • Not sure return response.data; is doing what you think it is doing. Commented May 22, 2013 at 13:33

3 Answers 3

2

You should do a little modification in your code

resolve: {
            myVar: function (repoService) {
                return repoService.getItems().then(function (response) {
                    return response.data;
                });
            }
        }


app.factory('repoService', function ($http) {
    return {
        getItems: function () {
            return $http.get('http://localhost:12008/getrepodata/jchannon');
        }
    };
});
Sign up to request clarification or add additional context in comments.

2 Comments

Cannot call method 'then' of undefined. looks like repoService.getItems() is undefined for some reason
im able to get the result perfectly using above methods if problem persists kindly set up a plunker of fiddle demo
1

I spent entirely too much time today trying to figure out how to get this to work. I found this thread in the angularjs group and reworked the example provided by Pawel Kozlowski.

Basically, the trick is to use an anonymous factory method in the resolve function. Otherwise, I guess you could just inject the function into the controller

Here's the plnkr

4 Comments

I've tried adding the 'then'to my controller but myVar gets injected as undefined into my controller. Any ideas?
have you seen this example on SO, it looks relevant: stackoverflow.com/questions/11972026/…
not sure how to do that as I reference my controller via string not object
Thanks will give it a go. I saw a similar approach here stackoverflow.com/questions/14980805/… but I get the error myVarProvider unknown
0

When working with resolves, you need to return the promise object. The view waits for the promise to resolve. Change your getItems function as follows.

app.factory('repoService', function ($http) {
    return {
        getItems: function () {
            var p = $http.get('http://localhost:12008/getrepodata/jchannon');
            p.then(function (response) {
                return response.data;
            });
            return p;
        }
    };
});

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.