javascript - 如何制作 JSON 的单独副本并阻止它在 angularjs 中被修改?

标签 javascript jquery json angularjs

要么我在这里犯了一个错误,要么可能滥用了 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/

相关文章:

javascript - 如何使用 jquery 更改 HTML 元素的 data-magnify-src 属性?

javascript - 如何在网页上初始化 JavaScript

javascript - Node : Is behavior well-defined for Promise. 都带有非 Promise 值?

javascript - 在JavaScript中使用 'prototype'和 'this'?

Javascript:使用下拉列表传递值

angularjs - 如何使用 AngularJS 在另一个对象中添加一个对象的 id

iphone - iPhone 中的 JSON 解析器出现问题?

javascript - 将下一个/上一个输入集中在达到最大长度或退格键上

javascript - 如何防止 dom 更改在 ajax 请求时重置

java - JSON 改变值