0

I'm trying to iterate through the JSON array and through the ingredients/directions array using ng-repeat. What I have isn't working. Any advice? Thanks!

Controller:

recipeControllers.controller('DetailsController', ['$scope', '$http','$routeParams',
function($scope, $http, $routeParams) {
$http.get('app/data.json').success(function(data) {
    $scope.recipe = data;
    $scope.whichItem = $routeParams.itemId;
    $scope.recipeIngredients = recipe[whichItem].ingredients;
}]);

HTML:

<div class="recipe">
    <div class="ingredients">
        <ul>
            <li ng-repeat="item in recipeIngredients">{{recipeIngredients[whichItem].ingredients}}</li>
        </ul>
     </div> 
</div>

JSON Data:

    [
  {
    "dish":"thai_chicken_satay",
    "ingredients": ["chicken", "sauce", "stuff"],
    "directions": ["step1", "step2", "step3"]
  },

  {
    "dish":"duck_confit",
    "ingredients": ["duck", "confit", "otherstuff"],
    "directions": ["step1", "step2", "step3"]
  }

]
6
  • is itemId a 0-indexed index of the item you want to show? or is it something else. Commented Apr 10, 2015 at 20:14
  • 2
    Where is the actual ng-repeat? Commented Apr 10, 2015 at 20:15
  • I'm a newbie programmer, so please excuse any obvious mistakes. Commented Apr 10, 2015 at 20:15
  • don't necessarily need an ng-repeat for this, though it is weird that the question mentions trying to use it but doesn't show it. Commented Apr 10, 2015 at 20:15
  • @KevinB, the question is about "trying to iterate ... using ng-repeat" Commented Apr 10, 2015 at 20:16

1 Answer 1

3

If you assign $scope.recipeIngredients properly (i.e. whichItem is properly set and maps to an actual object in your JSON data), then $scope.recipeIngredients already points to an array of ingredients (e.g. ["duck", "confit", "otherstuff"]), so to iterate you simply need to do:

<li ng-repeat="item in recipeIngredients">{{item}}</li>

If you need to iterate over the entire data array of recipes, then a nested ng-repeat is needed:

<div ng-repeat="recipe in recipes">
  <div>{{recipe.dish}}</div>
  <ul>
    <li ng-repeat="item in recipe.ingredients">{{item}}</li>
  </ul>
</div>
Sign up to request clarification or add additional context in comments.

8 Comments

whichItem maps to whatever recipe (JSON object) I selected from the previous page, so I'm not sure how to properly assign $scope.recipeIngredients
You arleady do that with $scope.recipeIngredients = recipe[whichItem].ingredients; (actually, it should be $scope.recipe[whichItem].ingre... or data[whichRecipe].ingre...)
For instance, if I just use <li>{{recipe[whichItem].ingredients}}</li>, then I'll get back the entire string on one line. I'd like each ingredient on its own line. Thanks so much for your help thus far.
@breken, and I gave you the answer... your $scope.recipeIngredients already points (if you fixed the typo/bug I mentioned) to the ingredients array - so, ng-repeat over that
I really appreciate your help, but it still doesn't seem to be working. I may be misreading your directions, however. Controller: $scope.recipe = data; $scope.whichItem = $routeParams.itemId; $scope.data[whichItem].ingredients = recipeIngredients; and in my HTML: <div ng-repeat="recipe in recipes"> <div>{{recipe.dish}}</div> <ul> <li ng-repeat="item in recipe.ingredients">{{item}}</li> </ul> </div>
|

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.