javascript - 使用angular JS检查数组是否为空的条件语句

标签 javascript arrays angularjs

我有这个功能:

$scope.doPaste = function(destination) {                            
   if ($scope.selectCopy.ids != []) {
       console.log("will copy");
       $scope.CopyFiles(destination);
   }
   if ($scope.selectMove.ids != []) {
       console.log("will move");
       $scope.MoveFiles(destination);
   }                                
};

在我的应用中,$scope.selectMove.ids$scope.selectCopy.ids 不能都是非空的。我的意思是,例如当 $scope.selectMove.ids 为非空时 $scope.selectCopy.ids 为空。

我的问题是,在控制台中,我总是看到 will copy 和 will move。

最佳答案

注意 [] != [] 返回 true(因为它们是不同的对象)。

您应该使用length 来检查数组是否为空。

if($scope.selectCopy.ids.length > 0){
     console.log("will copy");
     $scope.CopyFiles(destination);
}

关于javascript - 使用angular JS检查数组是否为空的条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828578/

相关文章:

python - PyCuda mem_alloc 初始化错误

javascript - React 表中的嵌套对象数组

angularjs - $location.search() 不起作用

javascript - AngularJS Provider 多实例

javascript - 如何在 onClick 函数内转换值?

javascript - 如何在 ng-bind-html 中呈现 Angular 变量的内容

JavaScript : How to add a node/element to a specific-indexed location?

javascript - ExtJs 窗口不显示

android - 创建标记数组 Google Maps V2

javascript - angular js中单个页面中的多个应用程序