Develop Develop -4 years ago 143
AngularJS Question

angularjs Array push Not working in switch statement

Angular js Push array not working in switch case it update last added element only

vm.saveModalData = function(title) {
if (!$scope.parentCtrl.docsList[vm.newDocObj.document_category]){
$scope.parentCtrl.docsList[vm.newDocObj.document_category] = [];
}
if(!title){
title = vm.docFileName;
$scope.parentCtrl.docsList[vm.newDocObj.document_category].push(vm.docFileName);
} else {
$scope.parentCtrl.docsList[vm.newDocObj.document_category].push(title);
}
var doc_obj = {};
doc_obj.title = title;
doc_obj.type = appConstants.DOCUMENT_CATEGORY[selectedCategory];
doc_obj.data = vm.newDocObj.document;
switch(selectedCategory) {
case 'PAN':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.pan_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Aadhaar':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.aadhar_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Driving License':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.driving_license_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Passport':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.passport_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Voter Id':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
$scope.parentCtrl.completeDetails.voter_id_document = true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+1].enabled=true;
$scope.parentCtrl.tabsList[$scope.parentCtrl.activeTab+2].enabled=true;
break;
case 'Company IT Returns':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
break;
case 'Company PAN':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
break;
case 'ITR/VAT Returns/ST Returns':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
break;
case 'Other':
$scope.parentCtrl.completeDetails.documents.push(doc_obj);
break;
default:
break;
}

vm.closeModal();
}


How to fix this issue?

I try to add first document it added into array then i added one more document but it remove existing and add as a new.

Answer Source

Try this,

You are calling docCtrl.openAddDocModal(doc) function, while adding each and every time of Document add. Inside that function only you have initialised your array.

$scope.parentCtrl.completeDetails.documents = [];

so that each and every time it's getting reinitialised with empty.

Add this code line out side of the $uibModal.open(), recommended add below $scope.arr = [];

hope it will work...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download