简体   繁体   English

在AngularJS中,哪里可以存储可更改对象的初始Object值?

[英]Where to store initial Object's values for the Objects that can be changed, in AngularJS?

I'm using a factory to create different user-Objects from data which comes from the Server. 我正在使用工厂根据来自服务器的数据创建不同的用户对象。 Each user-Object has a "userGroup"-property. 每个用户对象都有一个“ userGroup”属性。 A list of users is displayed using ng-repeat in the View. 在视图中使用ng-repeat显示用户列表。 And there it is possible to change the userGroup-value from "basic" to "admin", because of the AngularJS 1 two way binding. 由于AngularJS 1的两种方式绑定,因此可以将userGroup-value从“ basic”更改为“ admin”。 So the original values of the Object are gone. 因此,Object的原始值消失了。 So my question is: when we want to cancel the made changes where should the initial value {userGroup: "basic"} be stored? 所以我的问题是:当我们要取消所做的更改时,应将初始值{userGroup:“ basic”}存储在哪里? I was thinking about two possible solutions: 我在考虑两种可能的解决方案:

  1. create an additional property "initUserGroup" in the User Factory Class and store a value for each Object 在用户工厂类中创建一个附加属性“ initUserGroup”,并为每个对象存储一个值
  2. use localstorage (up to 20 records must be saved at once) 使用本地存储(一次最多可以保存20条记录)

Are there any best practices for such cases? 是否有针对此类情况的最佳做法?

For example, you can backup whole object in a property like _backup (using angular.copy) and restore if you with to undo changes (using angular.extend). 例如,您可以在_backup之类的属性中备份整个对象(使用angular.copy),如果要撤消更改(使用angular.extend),则可以进行还原。 Here is an example: 这是一个例子:

$scope.editItem(item) {
  item._backup = angular.copy(item);
}

$scope.undoEdit(item) {
  angular.extend(item, item._backup);
  //delete unused data
  delete(item._backup);
}

In this case you won't need to save data outside current object. 在这种情况下,您无需将数据保存在当前对象之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在开发人员控制台的AngularJS应用程序对象中的哪里可以找到声明的变量及其值? - Where to find declared variables and their values in the AngularJS application's object in the developer's console? 如何将更改的值存储到 JavaScript 对象中? - How do I store changed values into a JavaScript object? 是否需要存储对象,以便在刷新时可以在angularJS中使用该对象? - Need to store Objects, so that on refresh, the object should be available in angularJS? 如何使用AngularJS作为对象中的数组存储清单中的值? - How to store values from a checklist using AngularJS as an array in an object? 按另一个对象的值过滤对象数组 - Filter array of objects by another object's values 通过 object 过滤对象数组,其中 object 值的所有字段都得到尊重 - Filtering an array of objects by an object where all fields of the object values are respected AngularJS-检查模型的字段是否与初始值相同 - AngularJS - check if fields of model are the same with the initial values JavaScript对象在哪里存储它是否可扩展? - Where does a JavaScript object store whether it's extensible or not? 修改JS中的初始object值 - Modify initial object values in JS 突出显示在AngularJS中以编程方式更改的所有输入 - Highlight all inputs that where programatically changed in AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM