Angular - 在 ng-repeat 中迭代数组并从另一个数组中获取值
Angular - Iterate array inside ng-repeat and fetch values from another array
我有两个数组对象(数组 A 和数组 B),我正在基于此构建单个 angular table。
首先,我需要遍历数组 A 并填充几列,其他列基于数组 B。
我从数组 A 中获取键并传递键以从数组 B 中获取值。
请告诉我如何在 angular 中实现此目的?
<tbody>
<tr ng-repeat="a in arrayA">
<td> <b>{{$index+1}}</b> </td>
<td> <b>{{a.id}}</b> </td>
<td> <b>{{a.name}}</b> </td>
<td> {{a.number}} </td>
<td> This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
<td> This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
</tr>
</tbody>
试试这个
控制器
$scope.getValue = function (id) {
var returnData = '';
angular.forEach(arrayB,function(index){
if (index.id == id) {
returnData = index.name;
}
})
return returnData
}
html
<tbody>
<tr ng-repeat="a in arrayA">
<td> <b>{{$index+1}}</b> </td>
<td> <b>{{a.id}}</b> </td>
<td> <b>{{a.name}}</b> </td>
<td> {{a.number}} </td>
<td> {{getValue(a.id)}} This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
<td> This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
</tr>
</tbody>
我有两个数组对象(数组 A 和数组 B),我正在基于此构建单个 angular table。 首先,我需要遍历数组 A 并填充几列,其他列基于数组 B。 我从数组 A 中获取键并传递键以从数组 B 中获取值。
请告诉我如何在 angular 中实现此目的?
<tbody>
<tr ng-repeat="a in arrayA">
<td> <b>{{$index+1}}</b> </td>
<td> <b>{{a.id}}</b> </td>
<td> <b>{{a.name}}</b> </td>
<td> {{a.number}} </td>
<td> This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
<td> This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
</tr>
</tbody>
试试这个
控制器
$scope.getValue = function (id) {
var returnData = '';
angular.forEach(arrayB,function(index){
if (index.id == id) {
returnData = index.name;
}
})
return returnData
}
html
<tbody>
<tr ng-repeat="a in arrayA">
<td> <b>{{$index+1}}</b> </td>
<td> <b>{{a.id}}</b> </td>
<td> <b>{{a.name}}</b> </td>
<td> {{a.number}} </td>
<td> {{getValue(a.id)}} This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
<td> This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
</tr>
</tbody>