需要角度方向的ng-repeat?

时间:2017-05-29 10:48:59

标签: javascript angularjs angularjs-ng-repeat angular-ng-if

我对ng-repeat指令的结构很复杂,而我在循环中遇到困难。我只是在数组中发布数据表示 JSON格式

{"data":[
         ["sameer","1001",
          [
           {"button_icon":"fa fa-pencil",
            "button_tt":"create invoice",
            "button_color":"btn-success"
           },
           {"button_icon":"fa fa-pencil",
            "button_tt":"create invoice",
            "button_color":"btn-danger"
           }
          ]
         ],
         ["jack","1002",
          [
           {"button_icon":"fa fa-pencil",
            "button_tt":"create invoice",
            "button_color":"btn-success"
           },
           {"button_icon":"fa fa-pencil",
            "button_tt":"create invoice",
            "button_color":"btn-danger"
           }
          ]
        ]
}

它是一个数组(array(array())),最后一个内部数组是用于附加每行的按钮。我在附加按钮时卡住了。我已经在使用嵌套的ng-repeat,但是对于第三个问题很困惑。

这是我的HTML

<tbody>
    <tr ng-repeat = "row in list.data">

      <td ng-if = "(row.length - 1) != $index"  class="text-center"  ng-repeat = "val in row track by $index" ng-cloak>{{val}}</td>
      <td ng-if = "(row.length - 1) == $index" class="text-center">
          <div class="btn-group" ng-cloak>
            <a ng-repeat = "btn in row" data-toggle="tooltip" title="{{btn.button_tt}}" class="btn btn-xs {{btn.button_color}}" data-original-title="Edit" ng-click="viewExpense(1)"><i class="{{btn.button_icon}}"></i></a>
          </div>
        </td>
    </tr>

3 个答案:

答案 0 :(得分:2)

我相信你还需要一个循环。

row中的{p> row in list.data是第一级:

["sameer","1001",
      [
       {"button_icon":"fa fa-pencil",
        "button_tt":"create invoice",
        "button_color":"btn-success"
       },
       {"button_icon":"fa fa-pencil",
        "button_tt":"create invoice",
        "button_color":"btn-danger"
       }
      ]
 ]
btn中的{p> btn in row是第二级,也是一个数组:

[
    {"button_icon":"fa fa-pencil",
     "button_tt":"create invoice",
     "button_color":"btn-success"
    },
    {"button_icon":"fa fa-pencil",
      "button_tt":"create invoice",
      "button_color":"btn-danger"
    }
]

你应该像这样添加第三级:

<div class="btn-group" ng-cloak>
    <div ng-repeat = "innerRow in row">
        <a ng-repeat = "btn in innerRow" data-toggle="tooltip" title="{{btn.button_tt}}" class="btn btn-xs {{btn.button_color}}" data-original-title="Edit" ng-click="viewExpense(1)"><i class="{{btn.button_icon}}"></i></a>
    </div>
</div>   

希望有所帮助

答案 1 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<table>
    <tr ng-repeat="row in data.data">
        <td ng-if="(row.length - 1) != $index" class="text-center" ng-repeat="val in row track by $index" ng-cloak>
            {{val}}
        </td>
        <td ng-if="(row.length - 1) == $index" class="text-center">
            <div class="btn-group" ng-cloak>
                <div ng-repeat="innerRow in row">
                    <a ng-repeat="btn in innerRow" data-toggle="tooltip" title="{{btn.button_tt}}"
                       class="btn btn-xs {{btn.button_color}}" data-original-title="Edit" ng-click="viewExpense(1)"><i
                            class="{{btn.button_icon}}"></i></a>
                </div>
            </div>
        </td>
    </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
   $scope.data = {
  "data": [
    [ "sameer", "1001",
      [
        {
          "button_icon": "fa fa-pencil",
          "button_tt": "create invoice",
          "button_color": "btn-success"
        },
        {
          "button_icon": "fa fa-pencil",
          "button_tt": "create invoice",
          "button_color": "btn-danger"
        }
      ]
    ],
    [ "jack", "1002",
      [
        {
          "button_icon": "fa fa-pencil",
          "button_tt": "create invoice",
          "button_color": "btn-success"
        },
        {
          "button_icon": "fa fa-pencil",
          "button_tt": "create invoice",
          "button_color": "btn-danger"
        }
      ]
    ]
  ]
};
});
</script>

</body>

答案 2 :(得分:0)

以下是演示Jsfiddle

<强>的js

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

  app.controller('ctrl', function($scope) {
    $scope.list = {
      "data": [
        ["sameer", "1001", [{
          "button_icon": "fa fa-pencil",
          "button_tt": "create invoice",
          "button_color": "btn-success"
        }, {
          "button_icon": "fa fa-pencil",
          "button_tt": "create invoice",
          "button_color": "btn-danger"
        }]],
        ["jack", "1002", [{
          "button_icon": "fa fa-pencil",
          "button_tt": "create invoice",
          "button_color": "btn-success"
        }, {
          "button_icon": "fa fa-pencil",
          "button_tt": "create invoice",
          "button_color": "btn-danger"
        }]]
      ]
    };
  });

<强> HTML

    <div ng-app='myApp'>

      <div ng-controller='ctrl'>
        <div ng-repeat="row in list.data">
          {{row[0]}} {{row[1]}}
          <a href='#' ng-repeat='btn in row[2]'> {{btn.button_icon}} {{btn.button_tt}} {{btn.button_color}} <br></a>
          <hr>
        </div>
      </div>
    </div>

希望这会对你有所帮助