javascript - 如果它们共享相同的 id,Angular js ng-repeat 将用另一个范围数据替换一个值

标签 javascript angularjs

我对 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/

相关文章:

javascript - $rootScope :inprog $apply already in progress . 更改事件错误

javascript - 'innerHTML' : object is null or undefined

javascript - "can' t 创建隐藏全局属性的重复变量”

javascript - 如何在元素之间绑定(bind) css 属性?

javascript - 拦截 $resource 和 $http 响应 Angular

javascript - Firefox 在 ajax 调用 2 分钟后超时

javascript - Angular.js ng-repeat 按具有多个值之一的属性(或值)过滤

javascript - 当我们离开文本框时捕获事件

javascript - Typescript:重写子类中的父类属性

javascript - angularjs从外部 Angular 重定向