gpt4 book ai didi

jquery - 比较 AngularJS 中的两个模型

转载 作者:行者123 更新时间:2023-12-01 01:46:01 24 4
gpt4 key购买 nike

假设我有这些模型

第一个目标

{
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 模型应该自动刷新

最佳答案

我猜第一个和第二个目标模型都是数组。当推送到第二个目标时,只需检查数据是否已经存在于第二个目标中(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>

关于jquery - 比较 AngularJS 中的两个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24010061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com