首页 > 解决方案 > 如何将 ng-repeat 值或 ID 传递给我的附加元素?这可能吗?

问题描述

我有 ng 重复表,其中包含来自我的选择查询和数据条件的 5 个数据。我只想获取 ng-repeat 值和 ID 并将其传输到我的附加元素。我是Angularjs和HTML的新手,希望你们能提供帮助。感谢很多帮助。

这是我的 HTML:

<table class="table table-bordered kl">
   <thead>
      <tr>
         <th>ID</th>
         <th>VALUE</th>
      </tr>
   </thead>

   <tbody>
      <tr ng-repeat="d in mySelectData">
         <td>{{d.ID}}</td>
         <td>{{d.VALUE}} <p id="dataColor">Color</p> </td>
      </tr>
   </tbody>
</table>

这是我的控制器:

if(interval == 2000) {
    $('#dataColor').empty();
    $('#dataColor').append('<label> the color is {{d.VALUE}}</label>'
else {
    $('#dataColor').empty();
    $('#dataColor').append('<label> the color will be changed!</label>'
}

输出应该是:(在我的第一次加载中)

+--------+-----------+
|   ID   |   Value   |
+--------+-----------+
|   1    |   black   |
+--------+-----------+
|   2    |   red     |
+--------+-----------+
|   3    |   blue    |
+--------+-----------+
|   4    |   white   |
+--------+-----------+
|   5    |   red     |
+--------+-----------+

(在我的第二次加载或 2 秒后)

+--------+-----------------------------+
|   ID   |   Value                     |
+--------+-----------------------------+
|   1    |   black  the color is black |
+--------+-----------------------------+
|   2    |   red    the color is red   |
+--------+-----------------------------+
|   3    |   blue   the color is blue  |
+--------+-----------------------------+
|   4    |   white  the color is white |
+--------+-----------------------------+
|   5    |   red    the color is red   |
+--------+-----------------------------+

最后

+--------+-------------------------------------+
|   ID   |   Value                             |
+--------+-------------------------------------+
|   1    |   black  the color will be changed! |
+--------+-------------------------------------+
|   2    |   red    the color will be changed! |
+--------+-------------------------------------+
|   3    |   blue   the color will be changed! |
+--------+-------------------------------------+
|   4    |   white  the color will be changed! |
+--------+-------------------------------------+
|   5    |   red    the color will be changed! |
+--------+-------------------------------------+

标签: javascriptjqueryhtmlangularjssql-server

解决方案


您可以使用[数据属性][1]

将 data 属性添加到 html :

<tr ng-repeat="d in mySelectData">
    <td>{{d.ID}}</td>
    <td>{{d.VALUE}} <p id="dataColor-{{$index}}" class="js-color-change" data-color="{{d.VALUE}}">Color</p> </td>
</tr>

从 JS 中的数据属性中检索值

在你里面 if 条件你可以使用class而不是查询 ifid并通过使用数据属性来更改值


推荐阅读