为什么阵列保存最后的数据并且不清除?

Why array save last data and doesn't clears?

我有一个简单的AngularJs医疗卡申请

我有它的存储空间并使用 dx-datagrid:

在我的 home.html 上显示它

一张卡有很多记录,我通过cardId

recordsArray获取卡的记录
 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;

    }

现在我的记录就在第三张卡里。我在按钮上添加了一个功能来测试它:

 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!!!");
        }
    }
};

1,3,1 是 cardIdarray 的记录。但是,为什么array的卡记录没有清除和保存最后的数据?

有人知道我该如何解决吗?感谢您的回答!

P.S。我在我的应用程序中使用 ng-view 指令,我试图清除我的 array 使用另一个按钮:

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

但这没有帮助。

您应该在函数 getVardsRecordsByCardId 中初始化 cardsRecords 数组。

 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;
}