简体   繁体   English

AngularJS为动态表中的元素动态分配ID

[英]AngularJS dynamically assign IDs to elements in a dynamic table

Is there a way in AngularJS that I can dynamically ID or in a dynamic table? 在AngularJS中,有没有一种方法可以动态ID或动态表? I have this table that dynamically adds rows. 我有此表动态添加行。

 <table class="table table-striped">
                    <thead>
                        <tr>
                            <th style="text-align:center">{{nameBlue}}</th>
                            <th style="text-align:center">Round</th>
                            <th style="text-align:center">{{nameRed}}</th>
                        </tr>
                    </thead>
                    <tbody class="tablerows">
                        <tr ng-repeat="x in tableArray track by $index">
                          <td>Red Corner</td>
                          <td>Round {{$index}}</td>
                          <td>Blue Corner</td>
                        </tr>
                      </tbody>
                </table>

and my script 和我的剧本

 //Create the module
 var myApp = angular.module("myModule", []);



 //Create the controller and register the module in one line
 myApp.controller("myController", function ($scope) {
$scope.message = "AngularJS tutorial";
$scope.score = [1,2,3,4,5,6,7,8,9,10];
$scope.rounds = [1,2,3,4,5,6,7,8,9,10,11,12];
$scope.selectedRounds = 0;


$scope.tableArray = [];
$scope.getRoundsArray = function() {
     $scope.tableArray = new Array( $scope.selectedRounds * 1);
  }
 });

So the amount of rows in the table are dynamically selected and added. 因此,表中的行数是动态选择和添加的。 Red Corner and Blue Corner will be replaced with drop down lists which have a 1 to 10 value. 红色角和蓝色角将由具有1到10值的下拉列表取代。 At the end of the table I will sum the drop down list values so I want to be able to do math on round1Red + round2Red .. and so on. 在表的末尾,我将对下拉列表值进行求和,因此我希望能够对round1Red + round2Red ..等进行数学运算。 Is there a way I can dynamically assign IDs to each when the table is created? 创建表后,有没有一种方法可以为每个ID动态分配ID?

You can dynamically add IDs to the table like so: 您可以将ID动态添加到表中,如下所示:

 <tr ng-repeat="x in tableArray track by $index">
     <td id="redCorner{{$index}}">Red Corner</td>
     <td>Round {{$index}}</td>
     <td id="blueCorner{{$index}}">Blue Corner</td>
 </tr>

This should give you unique red and blue corner IDs for each row of the table in the form of redCorner0, blueCorner0, redCorner1... . 这应该以redCorner0, blueCorner0, redCorner1...的形式为表的每一行提供唯一的红色和蓝色角ID。 Let me know if this helps. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM