javascript - 如何将 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! |
+--------+-------------------------------------+
解决方案
您可以使用[数据属性][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>
在你里面 if 条件你可以使用class
而不是查询 ifid
并通过使用数据属性来更改值
推荐阅读
- javascript - 如何在滚动时更改导航栏的活动类
- sql - SQL JOIN,所有来自 t1 和所有来自 t2,除了 t1 的共同点
- heroku - 在 Heroku 付费测功机上启用空闲
- php - php Curl:错误:在 URL 中发现非法字符
- testing - 将 Cypress 更改为等待超过 20 秒的响应
- javascript - 用 key 合并两个数组,第二个数组是第一个数组中的对象
- python - Flask 的 HTML 中的麦克风输入
- python - 2.4版本的Tensorflow,pip安装tensorflow后是否要单独做tensorflow-gpu?
- arrays - 使用 Python 从 txt 文件中读取二维数组
- html - 来自 w3schools.com 的示例代码的奇怪 IE 行为