要么我在这里犯了一个错误,要么可能滥用了 Angularjs。我想在页面加载时制作一份 JSON 副本,使用原始表单创建表单,然后按下提交按钮,我将比较这 2 个 JSON 以检查它们是否相同,在此基础上我将访问数据库。
问题:无论我如何创建 JSON 的副本,当我更新原始 json 中的字段时,它也会在其副本中更新。因此,在按下提交按钮时,方法不会发现这 2 个 json 不同,因此不要点击数据库,数据不会被保存。
Code:
var globalJsonHRA =null;
//On succcess of $http
globalJsonHRA = data["3"];
$rootScope.jsonOfHRA = data["3"];
我的代码有什么问题吗?
更多详情: //这是在html页面上将值设置为JSON的地方
<tbody ng-repeat="obj in jsonOfHRA" my-Post-Repeat-Directive>
<tr class="BG8">
<td ><input type="text" ng-model="obj.sec10_decl_decl_val" id="rent_{{$index}}" ></td>
</tr>
</tbody>
//From making a copy I mean,data["3"] is set to 2 different JSONs, so they are copies of each other. For comparison I was using _.isEqual method of underscoreJs, But even I simply alert these 2 jsons ,I find them containing equal values.
最佳答案
使用 angular.copy
$rootScope.jsonOfHRA = angular.copy(data["3"]);
关于javascript - 如何制作 JSON 的单独副本并阻止它在 angularjs 中被修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26322652/