我有 ng 重复表,其中包含来 self 的选择查询和数据条件的 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>
这是我的 Controller :
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! |
+--------+-------------------------------------+
最佳答案
您可以使用[数据属性][1]
向 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>
Retrieve values from data attributes in JS
在您的 if 条件中,您可以使用 class
代替 if id
进行查询,并使用数据属性更改值
关于javascript - 如何将 ng-repeat 值或 ID 传递给我的附加元素?这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52983398/