我正在显示一个项目列表,每个项目旁边都有一个“编辑”按钮。单击可打开 Angular ui 模式窗口,用户可以更改特定项目的某些属性。
现在,让我烦恼的是,当在此编辑窗口中输入内容时,项目列表中的特定项目立即反映了更改。我只希望它在用户单击模式中的“确定”时更新,并且如果用户选择“取消”则根本不更改。
我的解决方法使用副本来制作所选项目的副本,然后将其用作视图的模型:
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()
在这里绝对合适。如果您想要更高级的东西,您可能需要结帐角度历史 https://github.com/decipherinc/angular-history
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)