使用AngularJS“copy()”避免参考问题

编程入门 行业动态 更新时间:2024-10-24 22:29:44
本文介绍了使用AngularJS“copy()”避免参考问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在显示一个项目列表,每个项目旁边都有一个编辑按钮。点击它会打开一个角度ui模态窗口,用户可以更改特定项目的某些属性。

I'm displaying a list of items, each of which has an "edit"-button next to it. A click on that opens an angular ui modal window and the user can change some properties of the specific item.

现在,让我感到困扰的是,在输入此编辑时 - 窗口,项目列表中的特定项目立即反映了更改。我只希望在用户点击模式中的确定时更新,如果用户选择取消则根本不更改。

Now, what bugged me was that when typing in this edit-window, the specific item in the list of items reflected the changes immediatly. I only wanted it to update when the user clicked 'ok' in the modal, and to not change at all if the user chose 'cancel'.

我的解决方法使用副本制作所选项目的副本,然后作为视图的模型:

My workaround uses copy to make a, well, copy of the chosen item that then serves as model for the view:

var modalInstance = $modal.open({ templateUrl: 'scripts/app/views/editBond.html', controller: function ($scope, $modalInstance, bond) { $scope.bond = angular.copy(bond); $scope.ok = function () { $modalInstance.close($scope.bond); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }, resolve: { bond: function () { return bond; } } });

使用适当的angular.copy()来避免此类问题?这是一个范围问题吗?

推荐答案

是的,使用 angular.copy( )绝对合适。如果你想要更高级的东西,你可能想要查看 angular-history

Yep, using angular.copy() is absolutely appropriate here. If you want something more advanced you might want to checkout angular-history

更多推荐

使用AngularJS“copy()”避免参考问题

本文发布于:2023-11-07 06:37:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565790.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AngularJS   copy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!