問題はこれです:
$scope.model1 = [1,2,3,4,5,6,7];
$scope.model2 = $scope.model1;
$scope.model2.splice(2, 1);
<pre>{{model1}}</pre>
<pre>{{model2}}</pre>
戻る:
[1,2,4,5,6,7]
[1,2,4,5,6,7]
必要:
[1,2,3,4,5,6,7]
[1,2,4,5,6,7]
なぜこうなった?
更新:
解決策:どこでも使用angular.copy($scope.val)
私のコードが悪い:
$scope.$watch('checkedImages', function (newVal) {
if (newVal !== undefined && newVal[0] !== undefined) {
if ($scope.model.curSupplier === undefined) {
$scope.model.curSupplier = newVal[0].supplier_id;
$scope.model.curCheckedImages = newVal;
}
$scope.supplier = newVal[0].supplier_id;
}
});
対
$scope.$watch('checkedImages', function (newVal) {
if (newVal !== undefined && newVal[0] !== undefined) {
if ($scope.model.curSupplier === undefined) {
$scope.model.curSupplier = angular.copy(newVal[0].supplier_id);
$scope.model.curCheckedImages = angular.copy(newVal);
}
$scope.supplier = angular.copy(newVal[0].supplier_id);
}
});