我对 js 和 angularjs 很陌生,遇到了一些我无法解决的基本问题。 我使用 ng-repeat 将 $scope.consignations 添加到 View 中。它是一个有好几层的大物体。在示例代码中,我将 $scope.consignations 分配给临时对象,之后,我在临时对象内部“导航”,并在某个时刻将数据推送到临时对象。它会按预期更改 View 。现在我想清除 $scope[elementName] obj,但它也会从 View 中清除推送的数据。我尝试删除临时引用(我假设它只是 $scope.consignations 对象的引用。)并且我无法再访问它,但是当我清除 $scope[elementName] 时,它无论如何都会清除 View 。
$scope.addElements = function(elementName){
temp=$scope.consignations;
for (var key in someArray) {
//here i "navigate" recursive inside temp
}
temp.push($scope[elementName]);
delete temp;
for (var key in $scope[elementName]) {
$scope[elementName][key]="";
}
};
最佳答案
temp=$scope.consignations
这样做你的 temp
& $scope.consignations
引用同一个对象。
因此任何对象的更改都会反射(reflect)在其他对象以及 View 中。
所以你应该像 $scope.temp=angular.copy($scope.consignations)
一样复制它然后使用 $scope.temp
用于 View 绑定(bind)和 $ scope.consignations
用于其他目的。
关于javascript - 删除javascript中的对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33622835/