1

I would like to load the content of one of two JSON files, food1.json or food2.json. I am trying to do this from the html template:

  <body ng-controller="MainCtrl" ng-init="init('food1')">

And then in the JS:

$scope.init = function (name) {
    $scope.name = name;
    $scope.category = name + ".json";
    $scope.foodlist = {};
    $http({
        method: 'GET',
        url: $scope.category,
    }).success(function (data, status, headers, config) {
        {
            $scope.foodlist = data;
        }
    }).error(function (data, status, headers, config) {
        // something went wrong :(
    });
};
});

The category name is properly assembled: I get "I am food1" if I print I am {{ category }}. But no food items are printed. I think I am doing the JSON call wrong.

Here's my Plunkr

6
  • use a console to look at errors...$http undefined error should have been a clue to problem Commented Oct 31, 2013 at 12:58
  • Plunkr dumps a lot of errors to the console in Chrome, so I haven't been using it in these experiments, but you're right. Commented Oct 31, 2013 at 13:13
  • Plunkr had nothing to do with it... error is in your code Commented Oct 31, 2013 at 13:15
  • Yes, that is true. But looking for console errors is not as useful if the console is full of, e.g " Uncaught SecurityError: Blocked a frame with origin "run.plnkr.co" from accessing a frame with origin "plnkr.co". Protocols, domains, and ports must match. Failed to load resource: the server responded with a status of 404 (Not Found) run.plnkr.co/7OJ6MtOoaLlT549f/plunkerPreviewTarget.json Uncaught SecurityError: Blocked a frame with origin "run.plnkr.co" from accessing a frame with origin "plnkr.co". Protocols, domains, and ports must match." Commented Oct 31, 2013 at 13:39
  • surely you don't develop in plunker Commented Oct 31, 2013 at 13:44

1 Answer 1

1

You have not injected $http in the controller. Change you code as

app.controller('MainCtrl', function($scope, $http) {

instead of

app.controller('MainCtrl', function($scope) {

DEMO

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

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.