如何检查 AngularJS 中是否不存在对象的属性?

How to check if an attribute of an object is not present in 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 是否没有 return 任何值。

我用过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);
                    }
                }