Angular.copy 内部承诺 angularjs
Angular.copy inside promise with angularjs
我需要复制一个对象,但这个对象在一个承诺中。
当我修改对象a时,对象b也被修改了。
我知道这可能是由于 async 模式有承诺,但我无法弄清楚。
简化我的代码,我有这样的东西:
承诺:
$scope.search = function () {
DocumentFactory.getDocuments(dataParams).then(function (data) {
makeFacets(data);
},
function (data) {
$scope.errorMessages.search = true;
});
};
它将搜索许多主题,并且 return 其中一些。
然后我创建一个包含该主题的数组:
var makeFacets = function(data) {
$scope.topics=[];
$scope.topics[0] ={father: "General", label:""};
$scope.topics[1] ={father: "International", label:""};
$scope.topics[2] ={father: "Crime", label:""};
$scope.topics[3] ={father: "NonLegal", label:""};
[...]
};
获得对象后,我用它来显示主题并在主题中进行过滤:
<div class="filter-box">
<input type="input" class="form-control" ng-model="filter.topic" ng-change="test1()">
</div>
<div class="filter-name" ng-repeat="data in topics">
<span>{{data.label}}</span>
</div>
我的最后一步是尝试在这些主题中进行过滤,为此,我需要复制这些主题。
$scope.allTopics = [];
$scope.test1 = function(){
if($scope.allTopics.length === 0){
angular.copy($scope.topics, $scope.allTopics);
}
$scope.topics = $scope.allTopics;
var filter = $scope.filter.topic;
if(filter.length>=3){
for(var i = 0; i<$scope.topics.length; i++){
if($scope.topics[i].children !== undefined){
for(var j = 0; j<$scope.topics[i].children.length; j++){
if($scope.topics[i].children[j].label.toLowerCase().indexOf(filter.toLowerCase()) === -1){
$scope.topics[i].children.splice(j, 1);
}
}
}
}
}
};
这当然行不通。我已经尝试了很多东西,但没有人工作。
我也试过在此处添加副本:
$scope.search = function () {
DocumentFactory.getDocuments(dataParams).then(function (data) {
makeFacets(data);
$scope.allTopics = [];
angular.copy($scope.topics, $scope.allTopics);
},
function (data) {
$scope.errorMessages.search = true;
});
};
每次我修改主题对象,所有主题对象也被修改。
问题是您在调用 copy
后进行赋值。因此,这两个变量引用了导致您观察到的问题的同一个对象。复制作业后将其删除,或者复制而不是作业。
if($scope.allTopics.length === 0){
angular.copy($scope.topics, $scope.allTopics);
}
$scope.topics = $scope.allTopics; // <-- this assignment is wrong
请注意,您还可以以更类似于赋值的方式使用复制:
$scope.topics = angular.copy($scope.allTopics);
我需要复制一个对象,但这个对象在一个承诺中。 当我修改对象a时,对象b也被修改了。 我知道这可能是由于 async 模式有承诺,但我无法弄清楚。
简化我的代码,我有这样的东西:
承诺:
$scope.search = function () {
DocumentFactory.getDocuments(dataParams).then(function (data) {
makeFacets(data);
},
function (data) {
$scope.errorMessages.search = true;
});
};
它将搜索许多主题,并且 return 其中一些。 然后我创建一个包含该主题的数组:
var makeFacets = function(data) {
$scope.topics=[];
$scope.topics[0] ={father: "General", label:""};
$scope.topics[1] ={father: "International", label:""};
$scope.topics[2] ={father: "Crime", label:""};
$scope.topics[3] ={father: "NonLegal", label:""};
[...]
};
获得对象后,我用它来显示主题并在主题中进行过滤:
<div class="filter-box">
<input type="input" class="form-control" ng-model="filter.topic" ng-change="test1()">
</div>
<div class="filter-name" ng-repeat="data in topics">
<span>{{data.label}}</span>
</div>
我的最后一步是尝试在这些主题中进行过滤,为此,我需要复制这些主题。
$scope.allTopics = [];
$scope.test1 = function(){
if($scope.allTopics.length === 0){
angular.copy($scope.topics, $scope.allTopics);
}
$scope.topics = $scope.allTopics;
var filter = $scope.filter.topic;
if(filter.length>=3){
for(var i = 0; i<$scope.topics.length; i++){
if($scope.topics[i].children !== undefined){
for(var j = 0; j<$scope.topics[i].children.length; j++){
if($scope.topics[i].children[j].label.toLowerCase().indexOf(filter.toLowerCase()) === -1){
$scope.topics[i].children.splice(j, 1);
}
}
}
}
}
};
这当然行不通。我已经尝试了很多东西,但没有人工作。
我也试过在此处添加副本:
$scope.search = function () {
DocumentFactory.getDocuments(dataParams).then(function (data) {
makeFacets(data);
$scope.allTopics = [];
angular.copy($scope.topics, $scope.allTopics);
},
function (data) {
$scope.errorMessages.search = true;
});
};
每次我修改主题对象,所有主题对象也被修改。
问题是您在调用 copy
后进行赋值。因此,这两个变量引用了导致您观察到的问题的同一个对象。复制作业后将其删除,或者复制而不是作业。
if($scope.allTopics.length === 0){
angular.copy($scope.topics, $scope.allTopics);
}
$scope.topics = $scope.allTopics; // <-- this assignment is wrong
请注意,您还可以以更类似于赋值的方式使用复制:
$scope.topics = angular.copy($scope.allTopics);