如何解决如何检查对象的属性是否在 AngularJS 中不存在?
if ($scope.cnr.cnrIndicator) {
// If billing already initiated,disable. Else,enable (by default)
if ($scope.cnr.billingInitiatedInd === true) {
$scope.display.createCNR = false;
$scope.cnr.reason = $filter('formatTextAreaString')($scope.cnr.reason);
}
else if (_.isUndefined($scope.cnr.billingInitiatedInd)) {
$scope.display.createCNR = false;
$scope.cnr.reason = $filter('formatTextAreaString')($scope.cnr.reason);
}
}
我必须检查 $scope.cnr.billingInitiatedInd
是否没有返回任何值。
我使用过 isUndefined
,但似乎不起作用。
解决方法
如果任何值预计未定义,您可以使用 !!! 来检查它。工作原理
let a = {b : 2}; //or null
console.log(!!!a.q)
//object a has no property q
//but it outputs true
所以在你的情况下你可以这样检查
if ($scope.cnr.cnrIndicator) {
// If billing already initiated,disable. Else,enable (by default)
if ($scope.cnr.billingInitiatedInd === true) {
$scope.display.createCNR = false;
$scope.cnr.reason = $filter('formatTextAreaString')($scope.cnr.reason);
}
else if (!!!$scope.cnr.billingInitiatedInd) {
$scope.display.createCNR = false;
$scope.cnr.reason = $filter('formatTextAreaString')($scope.cnr.reason);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。