javascript - AngularJS ng-repeat 显示来自 Json 的二维数组

标签 javascript arrays angularjs json

这是 html 文件,我想显示来自 json-$scope.listOfIngredient2D 数组

  <div class="ingredientMapping" ng-repeat="IngredientMapping in listOfIngredient track by $index">

      <ul>
        <!-- BEGIN: Inner ngRepeat. -->
        <li  ng-repeat="x in IngredientMapping.Ingredient">

            {{x.name}}

        </li>
        <!-- END: Inner ngRepeat. -->
    </ul>
      </div>

这是我的Controller.js

var myApp = angular.module('myApp', []);

myApp.controller('mainController',function ($scope, $http) {

$scope.listOfRecipe = null;
    var url = "http://164.132.196.117/chop_dev/recipe/recipes.json";
    var url2 = "http://164.132.196.117/chop_dev/recipe/recipes/view/";
$http({
method: 'GET',
url: url

}).then(function (response) {
         $scope.listOfRecipe = response.data.recipes;

         var temp;
         for(var i=0; i<response.data.recipes.length;i++){
             $http({
                        method: 'GET',
                        url: url2+ response.data.recipes[i].Recipe.id +".json"
                    }).then(function (response2) {

                        $scope.listOfIngredient = new Array(100);
                        for (var j = 0 ;j<100;j++)
                        {
                            $scope.listOfIngredient[i] = new Array(100);
                        }
                        for (var j = 0 ;j<response2.data.recipe.IngredientMapping.length;j++)
                        {
                            $scope.listOfIngredient[i][j] = response2.data.recipe.IngredientMapping[i];
                        }

                        console.log($scope.listOfIngredient);

                    });
         }



     })

这是我的 Json 文件 http://164.132.196.117/chop_dev/recipe/recipes/view/id.json

here is error when I implement it and I know this error is from      $scope.listOfIngredient[i][j] = response2.data.recipe.IngredientMapping,

我很困惑像这样获取二维数组是否正确 enter image description here

最佳答案

您的映射似乎已关闭。这对我有用:

<div ng-controller="MyCtrl">
  <div class="ingredientMapping" ng-repeat="recipes in listOfIngredient">
    <ul>
      <li  ng-repeat="x in recipes.recipe.IngredientMapping">
        {{x.Ingredient.name}}
      </li>
    </ul>
  </div>
</div>

http://jsfiddle.net/Lvc0u55v/7841/

编辑:

所以:

<li ng-repeat="x in recipes.recipe.IngredientMapping">而不是

<li ng-repeat="x in IngredientMapping.Ingredient">

和:

{{x.Ingredient.name}}而不是

{{x.name}}

关于javascript - AngularJS ng-repeat 显示来自 Json 的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684215/

相关文章:

javascript - 使用 AngularJS 1.x 使用 Object.observe 代替脏检查

javascript - 为什么无法在指令的 html 调用中访问 $rootScope?

arrays - 如何将基于日期项的数组数据分组到字典中

javascript - 访问没有元素的 Angular $scope

angularjs - Controller 中未定义 ng-model

javascript - jQuery 选择没有类或 ID 的元素

JavaScript:创建一个返回数组频率分布的函数

Java:如何计算String[]数组中具有两个相同连续字母的单词的分数

javascript - 带双点的 Angular 电子邮件验证

javascript - 使用 dojo 按键事件时无法避免最后一个字符输入