我必须将更新的数据与AngularJS中最初加载的数据进行比较。我尝试了以下方式,但我没有得到更新。
$http.get('users').then(function (resp) {
$scope.items = resp.data.items;
$scope.oitems = resp.data.items;
});
此处$scope.items
可以在表单中更新,
$scope.doneEditing = function () {
if (!angular.equals($scope.items, $scope.oitems)) {
alert('changed');
}
}
以我的方式,我无法跟踪初始数据的变化。我怎样才能做到这一点?
答案 0 :(得分:0)
如果你喜欢
$scope.items = resp.data.items;
$scope.oitems = resp.data.items;
$scope.items
和$scope.oitems
指向内存中的同一对象。因此,在您更改$scope.item
后,您将在$scope.oitems
中拥有相同的更改对象。
您需要为$scope.oitems
创建一个单独的对象initial
值为$scope.items
,以便您可以使用angular.copy();
<强>溶液强>
$http.get('users').then(function (resp) {
$scope.items = resp.data.items;
$scope.oitems = angular.copy($scope.items);
});
$scope.doneEditing = function () {
if (!angular.equals($scope.items, $scope.oitems )) {
alert('changed');
}
}