【问题标题】:ng-repeat in html tableng-repeat 在 html 表中
【发布时间】:2020-02-20 16:54:59
【问题描述】:

我正在制作一个 html 表格,我使用以下代码:

<table>
               <thead>
                   <tr>
                       <th colspan="2">The table header</th>
                   </tr>
               </thead>
               <tbody>
                  <tr ng-repeat="item in myDynamicLabels">
                    <td> {{item}}</td>
                   </tr>
                   <tr ng-repeat="value in myDynamicData track by $index" >
                       <td>{{value}}</td>
                   </tr>
              </tbody>
           </table>

我得到的输出是 物品 物品 物品 价值 价值 价值

但我希望它在列中,所以: 项目价值 项目价值 物品价值

我怎样才能解决这个问题并仍然使用 ng-repeat?

   angular.forEach(data.results.bindings, function(val)
       {
           $scope.myDynamicLabels.push(val.state.value);
           $scope.myDynamicData.push(val.nbr_university.value);

       })

这是我的标签和数据的 javascript 代码

【问题讨论】:

  • 能否请您将您拥有的数据类型放入 myDynamicLabels 和 myDynamicData 数组中。

标签: javascript html angularjs angularjs-ng-repeat


【解决方案1】:

如果标签与数据相关联,您可以与它们有一个映射,并在同一个 ng-repeat 中使用 item.label 和 item.value。

如果您不想要地图,则至少需要一种方法来以某种方式获取所需的值 getValueForLabel(item)。

但最后应该只有一个 ng-repeat

【讨论】:

  • angular.forEach(data.results.bindings, function(val) { $scope.myDynamicLabels.push(val.state.value); $scope.myDynamicData.push(val.nbr_university.value) ; }) 这就是我定义标签和数据的方式,我如何将标签与这些数据相关联?
【解决方案2】:

像这样修改你的代码:

 angular.forEach(data.results.bindings, function(val)
       {
           let jsonData = {
             state: val.state.value,
             university: val.nbr_university.value
           }
           $scope.myDynamicData.push(jsonData);

       })

然后像这样使用 ng-repeat:

<table>
   <thead>
      <tr>
        <th colspan="2">The table header</th>
      </tr>
   </thead>
   <tbody>
      <tr ng-repeat="value in myDynamicData track by $index" >
        <td>{{value.state}}</td>
        <td>{{value.university}}</td>
      </tr>
   </tbody>
</table>

希望这是你愿意做的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    相关资源
    最近更新 更多