0

I have created the following controller in Angular, and I'm trying to access some child objects in the returned JSON.

Controller:

var app = angular.module("MyApp", []);

app.controller("FAQCtrl", function($scope, $http) {
    $scope.dataLoaded = false;
              $http.get('config/data.json', {
      })
    .success(function(data, status, headers, config) {
      $scope.config = data;
      $scope.dataLoaded = true;  
    }).
    error(function(data, status, headers, config) {
      window.alert("Sorry! There has been a problem!\n\nPANIC!")
    });
});

The JSON that's returned is in this format :

{
"entryList":
{
    "totalEntries": 237,
    "incEntries": 10,
    "offset": 0,
    "loggingURL": "/action/logStatistics?action1=%27client%d%27",
    "entry":
[
        {
            "catID": "delivery",
            "entryID": "delivery-charges",
            "subcatID": "delivery-options",
            "question": "What are your delivery charges?"
},
        {
            "catID": "returns-and-refunds",
            "entryID": "online-returns-policy",
            "subcatID": "returns-policy",
            "question": "What is your online returns policy?"
},
        {
            "catID": "delivery",
            "entryID": "track-delivery",
            "subcatID": "order-status",
            "question": "How can I track my delivery/order?"
},
        {
            "catID": "delivery",
            "entryID": "click-and-collect",
            "subcatID": "delivery-options",
            "question": "Do you offer Click and Collect or Collect +?"
},
        {
            "catID": "products-and-services",
            "entryID": "makeover",
            "subcatID": "services",
            "question": "Can I come in for a free makeover and skin consultation?"
},
        {
            "catID": "delivery",
            "entryID": "how-long-for-delivery",
            "subcatID": "delivery-queries",
            "question": "How long will it take for my products to be delivered?"
        }
    ]
}

}

I'd like to be able to return the details of each of the "entry" sections of the feed, but I'm struggling to make the following work:

<div ng-repeat="item in entryList">
<a href="" class="btn btn-info btn-lg btn-block">{{item.entryID}}</a>
<br />
</div>

Could any one give me an idea on how I can easliy access the child sections?

Thanks

2 Answers 2

3

There seem to be several problems here.

  1. You are referring to entryList in ng-repeat but you don't have a variable called entryList in your scope. Once your data is returned you will need to assign $scope.entryList = data.entryList.entry;in the success portion.
  2. If this is data that is required by the directive on your page on load then you should pass it into your controller rather than populating it after the controller is loaded. This can be done by using resolve. Here's more info on that https://thinkster.io/egghead/resolve
Sign up to request clarification or add additional context in comments.

Comments

1

On your ajax call you're storing all data at $scope.config so either store them elsewhere or use config on ng-repeat

<div ng-repeat="item in config.entryList.entry">
<a href="" class="btn btn-info btn-lg btn-block">{{item.entryID}}</a>
<br />
</div>

Otherwise if you want to store it on another variable than :

var app = angular.module("MyApp", []);

app.controller("FAQCtrl", function($scope, $http) {
    $scope.dataLoaded = false;
              $http.get('config/data.json', {
      })
    .success(function(data, status, headers, config) {
      $scope.config = data;
      $scope.entrylist = data.entryList.entry;
      $scope.dataLoaded = true;  
    }).
    error(function(data, status, headers, config) {
      window.alert("Sorry! There has been a problem!\n\nPANIC!")
    });
});

1 Comment

@charlietfl I'm not getting this quite well ?

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.