使用angularjs创建动态tr,td的动态html表

时间:2015-08-20 06:15:03

标签: html angularjs

如何从json文件创建一个html表,我不知道列的数量或行数(ng-row行数足够多), 这个json文件是可编辑的,列数和行数改变

1 个答案:

答案 0 :(得分:0)

您需要使用$http$resource服务将JSON文件加载到您的应用中。这最好在服务中完成,您可以在需要数据时注入该服务。

this.getJson = function() {  // real json get
   $http.get('/api/GetJson').then(function(data) {
      return data;
});

};

我创建了一个small plunker,其中包含一个包含假冒getJson函数的服务,该函数返回带有行的json数据。然后我将服务注入我的HomeController并将行放在$ scope上。

 $scope.rows = loadJsonService.getFakeJson().rows;

当行在$ scope上时,您只需使用ngRepeat指令来创建表结构。

<table class="table table-striped">
    <tr class="row" ng-repeat="row in rows">
      <th>row {{$index + 1}}</th>
      <td class="column" ng-repeat="column in row.column">{{column}}</td>
    </tr>
  </table>
相关问题