[英]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:
我在考虑两种可能的解决方案:
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.