Angular - 在ng-repeat中迭代数组并从另一个数组中获取值

时间:2017-02-14 10:21:53

标签: javascript angularjs arrays angularjs-ng-repeat ng-repeat

我有两个数组对象(数组A和数组B),我正在构建基于此的单个角度表。 首先,我需要迭代数组A并填充几列,其他列基于数组B. 我从数组A中获取密钥并传递密钥以从数组B中获取值。

请让我知道如何以角度实现这一目标?

<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>

1 个答案:

答案 0 :(得分:2)

试试这个

控制器

$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>