如何修复布尔检查并检查var是否未定义

时间:2019-05-02 07:44:33

标签: jquery angularjs

我正在编写一个代码,需要检查$scope.bankregel$scope.showInvoices是对还是假。

在函数if和else上,如果我已经在检查条件。如何在if和else if中添加额外的条件?

这是我尝试过的

    $scope.filterItems = function(){
    if($scope.bankregel || $scope.banklines != undefined){
        var begin = (($scope.curPage - 1) * $scope.itemsPerPage),
            end = begin + $scope.itemsPerPage;

        $scope.filteredItems = $scope.banklines.slice(begin, end);
    }
    else if ($scope.showInvoices || $scope.invoices != undefined){
        var begin = (($scope.curPage - 1) * $scope.itemsPerPage),
            end = begin + $scope.itemsPerPage;

        $scope.filteredItems = $scope.invoices.slice(begin, end);
    }
}

我希望有人能帮助我,告诉我我在做错什么。

2 个答案:

答案 0 :(得分:1)

$scope.filterItems = function(){
    if(typeof $scope.banklines !== "undefined" && $scope.bankregel){
        var begin = (($scope.curPage - 1) * $scope.itemsPerPage),
            end = begin + $scope.itemsPerPage;

        $scope.filteredItems = $scope.banklines.slice(begin, end);
    }
    else if ( typeof $scope.invoices !== "undefined" && $scope.showInvoices){
        var begin = (($scope.curPage - 1) * $scope.itemsPerPage),
            end = begin + $scope.itemsPerPage;

        $scope.filteredItems = $scope.invoices.slice(begin, end);
    }
}

答案 1 :(得分:0)

尝试

$scope.filterItems = function(){
    if($scope.bankregel && $scope.banklines != undefined){
        var begin = (($scope.curPage - 1) * $scope.itemsPerPage),
            end = begin + $scope.itemsPerPage;

        $scope.filteredItems = $scope.banklines.slice(begin, end);
    }
    else if ($scope.showInvoices && $scope.invoices != undefined){
        var begin = (($scope.curPage - 1) * $scope.itemsPerPage),
            end = begin + $scope.itemsPerPage;

        $scope.filteredItems = $scope.invoices.slice(begin, end);
    }
}