0

I want to parse JSON in AngularJS. I use $stateParams in this controller

 rerunApp.controller('rerunCategoryListCtrl', function($scope, $http, $stateParams) {
     var stpNameCat = $stateParams.nameCat;
             $http.get(JSON URI).success(function (data, status) {
                  var response = data.Items.stpNameCat;
                  console.log(response);
             });
    });

My JSON is below.

{
   cacheFileUpdate: 1435651202,
 - Items: {
       + newsProgram: [...],
       + entertainProgram: [...],
       + documentaryProgram: [...],
       + benefitProgram: [...],
       + kidsProgram: [...],
       + dramaProgram: [...],
       + oldProgram: [...],
       + etcProgram: [...]
   }
}

When I run this app. I want to get item in each Items such as $stateParams is newsProgram, output is item in newsProgram. But it error at data.Items.stpNameCat. How can I fix it or have some method ? Thanks a lot.

1 Answer 1

1

If your key is a variable, you use data.Items[stpNameCat] to access it instead of data.Items.stpNameCat.

I think you should pass the category to the server in $http and have it return the data you need only. But that is your design choice.

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.