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);
                        }
                    }

I have to check if $scope.cnr.billingInitiatedInd doesn’t return any value.

I have used isUndefined, but that doesn’t seem to work.

Answer

if any value is expected to be undefined, you can check it with !!!. How it works

let a = {b : 2}; //or null
console.log(!!!a.q) 
//object a has no property q 
//but it outputs true

So in your case you can check it like this

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);
                    }
                }

Leave a Reply

Your email address will not be published. Required fields are marked *