使用AngularJS“copy()”避免参考问题 [英] Using AngularJS "copy()" to avoid reference issues
问题描述
我正在显示一个项目列表,每个项目旁边都有一个编辑按钮。点击它会打开一个角度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()”避免参考问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!