Vladimir Khodakovskey Vladimir Khodakovskey - 6 months ago 12
AngularJS Question

Why array safe last data and don't clearns?

i have a simple AngularJs application of mediacal cards, i have storage with it and display it at my home.html use dx-datagrid:
enter image description here
One Card has many records, i get records of card from recordsArray by cardId

getVardsRecordsByCardId: function (id, recordsArray) {
if (recordsArray.length != 0) {
for (var i = 0; i < recordsArray.length; i++) {
if (recordsArray[i].cardId === id) {
cardsRecords = cardsRecords.concat(recordsArray[i]);
}
}
}

return cardsRecords;

}


Now have records just third card, i added function to button for test it:

var jdskla = [];
var localCardId = 0;
$scope.showCardDetails = {
text: "",
type: "default",
icon: "preferences",
onClick: function () {
if ($scope.itemIdFromShowButton) {
$location.path('/carddetail/' + $scope.itemIdFromShowButton);
var jdskla =[];
var jdskla = businessLogicOfMyApp.getVardsRecordsByCardId($scope.itemIdFromShowButton, $scope.recordsArray);
console.log($scope.itemIdFromShowButton)
console.log(jdskla);

}
else {
alert("Error!!!");
}
}
};


enter image description here

1,3,1 is cardId's and array of records, but, why array of card records don't clearns and safe last data ?May be somebody know how i can resolve it ? Thanks for your answers!
P.S. i use ng-view directive in my app and i tryed clear my array use anothe button:

$scope.backToGeneralPage = {
text: "Back",
onClick: function () {
jdskla = [];
$location.path('/');
}
};


but it don't helpful.

Answer

You should initialize cardsRecords array in function getVardsRecordsByCardId.

 getVardsRecordsByCardId: function (id, recordsArray) {
    var cardsRecords = []; // initialize array locally
    if (recordsArray.length != 0) {
        for (var i = 0; i < recordsArray.length; i++) {
            if (recordsArray[i].cardId === id) {
                cardsRecords.push(recordsArray[i]);
            }
        }
    }
    return cardsRecords;
}