使用ng-repeat与$index遍历对象数组渲染表格时最后一列不显示
Hey there! Let's get that third column showing your parameters array properly. The issue comes down to how you're accessing your data in the ng-repeat and not properly iterating over the parameters array for each row.
What Was Wrong in Your Original Code
Your current ng-repeat uses rows in $ctrl.name (which doesn't match your actual data structure) and you're trying to access properties via $ctrl.name[$index]—this is misaligned with your myArray which contains objects with name, id, and parameters properties. Also, the third column wasn't set up to loop through the parameters array.
Corrected Code Example
Here's the fixed version that will render all three columns correctly:
<table> <thead> <tr> <th>Name</th> <th>ID</th> <th>Parameters</th> </tr> </thead> <tbody> <!-- Iterate over your actual myArray, each "item" is an object in the array --> <tr ng-repeat="item in $ctrl.myArray track by item.id"> <td>{{item.name}}</td> <td>{{item.id}}</td> <td> <!-- Loop through the parameters array for each item --> <span ng-repeat="param in item.parameters track by $index"> {{param}} <!-- Add a comma separator between parameters (except the last one) --> <span ng-if="!$last">, </span> </span> </td> </tr> </tbody> </table>
Key Improvements
- Proper Data Access: Instead of using
$indexto reach into the array, we directly use theitemvariable from the ng-repeat to accessitem.name,item.id, anditem.parameters—this is cleaner and matches your data structure. - Iterate Over Parameters: The inner
ng-repeatloops through each parameter in theitem.parametersarray, so every value gets displayed. - Cleaner Track By: Using
track by item.id(since id is unique) is better practice thantrack by $index—it helps Angular render rows more efficiently when the array changes.
This should make your third column display all the parameters for each row as expected!
内容的提问来源于stack exchange,提问作者dadsa

