I am using angularjs 1.5 and in that trying to copy one object in another variable. To copy the variable, I am using angular.copy() function . The destination variable is not getting all the values which is there in source.
Below is my code
$scope.searchCond = {
group_id:[],
sections:[]
};
for(var i=1;i<5;i++) {
$scope.searchCond.sections[i+"_sec"]=[];
$scope.searchCond.sections[i+"_sec"]["section_id"]=[];
$scope.searchCond.sections[i+"_sec"]["section_id"].push(i);
};
var tmpVar = angular.copy($scope.searchCond);
console.log(tmpVar);
console.log($scope.searchCond);
Output of both the console are given below
OutPut of $scope.searchCond
{group_id: Array(0), sections: Array(0)}
group_id:[]
sections:Array(0)
1_sec:[section_id: Array(1)]
2_sec:[section_id: Array(1)]
3_sec:[section_id: Array(1)]
4_sec:[section_id: Array(1)]
OutPut of tmpVar
{group_id: Array(0), sections: Array(0)}
group_id:[]
sections:Array(0)
length:0
tmpVar is not copying sections(1_sec,2_sec) from the source object $scope.searchCond
Is there any solution for this problem?
$scope.searchCond = {
group_id:[],
̶s̶e̶c̶t̶i̶o̶n̶s̶:̶[̶]̶
sections:{}
};
for(var i=1;i<5;i++) {
̶$̶s̶c̶o̶p̶e̶.̶s̶e̶a̶r̶c̶h̶C̶o̶n̶d̶.̶s̶e̶c̶t̶i̶o̶n̶s̶[̶i̶+̶"̶_̶s̶e̶c̶"̶]̶=̶[̶]̶;̶
$scope.searchCond.sections[i+"_sec"]={};
$scope.searchCond.sections[i+"_sec"]["section_id"]=[];
$scope.searchCond.sections[i+"_sec"]["section_id"].push(i);
};
var tmpVar = angular.copy($scope.searchCond);
console.log(tmpVar);
console.log($scope.searchCond);
The angular.copy function only copies the numeric properties of an array. If you want the property names to be non-numeric, initialize it to be an object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.