【问题标题】:Comparing two models at AngularJS在 AngularJS 比较两个模型
【发布时间】:2014-06-03 08:06:17
【问题描述】:

假设我有这些模型

第一目标

{
  GoalId: 30
  GoalDesc: "The quick brown fox"
},
{
  GoalId: 31
  GoalDesc: "The quick brown fox junior"
},{
  GoalId: 32
  GoalDesc: "The quick brown fox senior"
}

第二个目标

{
  GoalId: 30
  GoalDesc: "The quick brown fox"
},
{
  GoalId: 35
  GoalDesc: "The big bad fox"
},{
  GoalId: 36
  GoalDesc: "The little red fox"
}

这些是我的 html:

<table>
       <tr ng-repeat="goal in firstGoals" id="choose-goals-row-{{goal.GoalId}}">
            <td><input type="checkbox" id="selection-chkbox-{{goal.GoalId}}" ng-model="secondaryGoals[$index].selected" ng-true-value="true" ng-false-value="false" style="float:left;margin-left:0px;"/></td>
            <td>{{goal.GoalDesc}}</td>
        </tr>
    </table>

<table>
       <tr ng-repeat="goal in secondGoals" id="choose-goals-row-{{goal.GoalId}}">
            <td><input type="checkbox" id="selection-chkbox-{{goal.GoalId}}" ng-model="secondaryGoals[$index].selected" ng-true-value="true" ng-false-value="false" style="float:left;margin-left:0px;"/></td>
            <td>{{goal.GoalDesc}}</td>
        </tr>
    </table>

如何使用过滤比较这两个模型?我需要通过其 GoalId 属性使用 jQuery 和 AngularJS 在第二个模型“secondGoals”中不存在的第一个模型“firstGoals”对象?

firstGoals 应该是实时的或可观察的(我正在使用最新的 AngularJS 库) 我现在正在做的是,如果我点击删除按钮,我在 firstGoals 模型中选择的目标将被转移(推送和拼接方法)到 secondGoals反之亦然,删除它是批量添加/删除对象/数组。

附加:应该在 firstGoals 显示的所有内容都应该在 secondGoals 不存在,因为我们正在以相同格式拼接和推送批量对象。我是firstGoals 模型的思考应该是自动刷新

【问题讨论】:

标签: jquery angularjs


【解决方案1】:

我猜第一个和第二个目标模型都是数组。 当推入 secondGoals 时,只需检查数据是否已经存在于 secondGoals (secondgoals.indexOf(data)==-1) 中,如果没有推入它。同样适用于 firstGoals

scope.pushData = function(from, to, data){
  if(to.indexOf(data) == -1){
    to.push(data);
  }
  from.splice(from.indexOf(data),1);
}

<div ng-click='pushData(firstGoals, secondGoals, data)></div>

【讨论】:

  • 您是一个一个地添加/删除多个对象还是批量添加/删除多个对象。无论哪种方式,您都可以在 ng-repeat 中包含 add/remove 选项,如果您是一个一个地执行它,或者在批量循环通过必须添加或删除的对象并调用 pushData 函数的情况下。您能否提供一个示例 plunker 代码,说明您希望它是怎样的?
猜你喜欢
  • 2017-01-24
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2016-06-14
  • 2020-05-27
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多