2

I am trying to display data from a JSON file onto the webpage. My Code

angular.module('bioA.configs',['ngRoute'])
//Config routes
.config(['$routeProvider',function($routeProvider){
    $routeProvider.when('/',
    {
        templateUrl: 'list.tpl.html',
        controller: 'BioACtrl',
        resolve: {
            bioAList: ['$http',function($http){
                return $http.get('bioa.json');
            }]
        }
    });
}]);
//Controller to Manage the data
angular.module('bioA.controllers',['bioA.configs'])
.controller('BioACtrl',['bioAList','$scope',function($scope,bioAList){
    console.log(bioAList);
    $scope.samples = bioAList.data.samples;
}]);
angular.module('bioA',['ngRoute','bioA.controllers','bioA.configs','ui.bootstrap']);

The $http doesnt seem to resolve. here is the console output: Console output I am AngularJS noob any help is appreciated :) I am stuck. Why is the resolved object a ChildScope rather than being a promise ?

6
  • Please provide the applicable HTML. Commented Mar 7, 2014 at 22:40
  • from your error you get undefined samples but not data. Sounds like bioAList has been created but there is now data Commented Mar 7, 2014 at 22:41
  • @onaclov2000 Here is the plunk Commented Mar 7, 2014 at 22:49
  • this seems to be an async request so you need to listen for a success or failure not just return $http.get('bioa.json'); Commented Mar 7, 2014 at 23:33
  • @emailnitram I have updated the plunk to do that the status returned is 200 on the console, which means the data should be available in the controller(?). Commented Mar 7, 2014 at 23:43

1 Answer 1

1

first of all, your includes are reversed here

controller('BioACtrl',['bioAList','$scope',function($scope,bioAList){

Should be

controller('BioACtrl',['$scope', 'bioAList',function($scope,bioAList){

Second, you are trying to access data in your bioAList service before you even fetch it. The correct way to do this is with angular promises. I modified the plnkr to acheive this access paradigm:

bioAList.getSamples().then(function(data) {
    $scope.samples = data.samples;
})

EDIT: add the thing that @OdeToCode points out.

Good point! you can just return the $http promise as the service.

return $http.get('bioa.json').success(function(data,status){
    return data;
})

And access it like you originally had.

Hope this helps!

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

2 Comments

You can write less code if you avoid using $q. The return values from then or success will be wrapped in a promise. You also don't need to resolve the promise in the controller as that is what the resolve function is designed to do. See:plnkr.co/edit/zaOYzlCFKqVYF9ycs9aG?p=preview
thanks a lot!! just fixing the dependency order fixed it. @odetocode Yes I dont think we need to use $q as $http returns a promise after resolve.

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.