javascript - 如何将 ng-repeat 值或 ID 传递给我的附加元素?这可能吗?

标签 javascript jquery html angularjs sql-server

我有 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/

相关文章:

javascript - 定位具有动画效果的元素(jQuery UI)

javascript - React JS onClick 改变正在渲染的内容

javascript - 谷歌地图、融合表和标记

javascript - 变量内部的 jQuery 选择器?

javascript - onpageshow 和 onpagehide 听众工作的问题

javascript - 根据用户编辑的表值创建新数组

javascript - 在javascript中根据价格和类别复选框隐藏和显示div

jquery - 如何将其编写为函数

javascript - onclick 按钮,我希望按钮被着色并存储一个值

html - 我可以使用自定义构建的 Angular 组件作为输入字段的占位符吗