我对 Angular js 很陌生,所以这对你们来说可能是一个简单的解决方案。
我有两个数据源,一个来自数据库,第二个来自第三方API。在 ng-repeat 内部,我尝试将当前源的一个值替换为第二个源(如果它们的 id 匹配)。我只想在 View 中显示,因此我不需要更新数据库。
<tr data-ng-repeat="affiliate in first_source">
<td>{{affiliate.affiliate_id}}</td>
<td>${{affiliate.payout | number:2}}</td>#I NEED TO REPLACE THIS VALUE FROM SECOND SOURCE if their id matches.
</tr>
**Origina Source example**
{affiliate_id:1,payout:2,type=test....}
**API SOURCE Example**
{affiliate_id:1,payout:5},{affiliate_id:2,payout:1}
Original outcome
<td>1</td>
<td>2</td>
<td>test</td>
Required Outcome
<td>1</td>#from original source
<td>5</td>#from second source
<td>test</td>#from original source
任何建议
最佳答案
正如评论中所指出的,您可以使用过滤器、指令、 Controller 函数来完成此操作。
下面是一种方法。这个在模板中使用 ng-if 指令执行此操作。
<tbody>
<tr ng-repeat="affiliate in firstSource">
<td>{{affiliate.id}}</td>
<td ng-if="affiliate.id==secondSource[$index].id">{{secondSource[$index].payout}}</td>
<td ng-if="affiliate.id!=secondSource[$index].id">{{affiliate.payout}}</td>
<td>{{affiliate.type}}</td>
</tr>
</tbody>
您可以在此处预览工作版本:
http://plnkr.co/edit/Fze7jcU5Nm3SKlgK4IIU?p=preview
如果数组可以有不同的顺序,最好调用 Controller 函数。演示如下:
http://plnkr.co/edit/1Jr1B3QL7NPX9nhMfH9x?p=preview
关于javascript - 如果它们共享相同的 id,Angular js ng-repeat 将用另一个范围数据替换一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24658892/