admin管理员组文章数量:1205762
I have this function:
$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);
}
};
In my app, $scope.selectMove.ids
and $scope.selectCopy.ids
can't be both non-empty. I mean for example when $scope.selectMove.ids
is non-empty $scope.selectCopy.ids
is empty.
My problem is that in the console, I always see both will copy and will move.
I have this function:
$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);
}
};
In my app, $scope.selectMove.ids
and $scope.selectCopy.ids
can't be both non-empty. I mean for example when $scope.selectMove.ids
is non-empty $scope.selectCopy.ids
is empty.
My problem is that in the console, I always see both will copy and will move.
Share Improve this question edited Apr 23, 2015 at 16:21 Yuri asked Apr 23, 2015 at 15:55 YuriYuri 4473 gold badges9 silver badges20 bronze badges 1 |5 Answers
Reset to default 10Note [] != []
return true
(because they are different objects).
You should use length
to check whether an array is empty.
if($scope.selectCopy.ids.length > 0){
console.log("will copy");
$scope.CopyFiles(destination);
}
I think you should check by angular.isObject()
which would return true if it is an object.
$scope.doPaste = function(destination) {
if (angular.isObject($scope.selectCopy.ids) && $scope.selectCopy.ids.length > 0) {
console.log("will copy");
$scope.CopyFiles(destination);
}
if (angular.isObject($scope.selectMove.ids) && $scope.selectMove.ids.length > 0){
console.log("will move");
$scope.MoveFiles(destination);
}
};
You have to check for null or undefined values.
$scope.doPaste=function(destination) {
if ($scope.selectCopy.ids && $scope.selectCopy.ids.length > 0) {
console.log("will copy");
$scope.CopyFiles(destination);
}
if ($scope.selectMove.ids && $scope.selectMove.ids.length > 0) {
console.log("will move");
$scope.MoveFiles(destination);
}
};
May be you need to use if else
condition:
if (empty){
console.log('empty');
}else{
console.log('not empty');
}
in your code. it is some like this:
$scope.doPaste=function(destination) {
if ($scope.selectCopy.ids && $scope.selectCopy.ids.length > 0) {
console.log("will copy");
$scope.CopyFiles(destination);
}
else {
console.log("will move");
$scope.MoveFiles(destination);
}
};
If you want to make sure it's an Array with at least one element inside, make a small function for checking that. ( maybe you'll want to extend that check later )
var isNonEmptyArray = function(ar){
return Array.isArray(ar) && (ar.length > 0);
};
$scope.doPaste=function(destination){
if( isNonEmptyArray($scope.selectCopy.ids) ){
console.log("will copy");
$scope.CopyFiles(destination);
}
if( isNonEmptyArray($scope.selectMove.ids) ){
console.log("will move");
$scope.MoveFiles(destination);
}
};
Also avoid the weak !=
operator, use the strict one !==
.
And comparing to []
is not helpful, []
will always return a new Array.
本文标签: javascriptConditional statement to check if an array is empty with angular JSStack Overflow
版权声明:本文标题:javascript - Conditional statement to check if an array is empty with angular JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738702453a2107739.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.length
property! – Dhaval Marthak Commented Apr 23, 2015 at 15:57