0

I have the following element.

<div> My element</div>

and the following object:

$scope.myObject = {'life_log' : [{status: 'ALIVE'},{status: 'DEAD'}]}

How can I display the element ONLY if all status is ALIVE.

I know how to use ng-show on a variable but what about a condition like this?

1
  • In general you'd probably use ng-if but there could be a different way depending on how you are using myObject.life_log? In an ng-repeat where you'll iterate through all of the objects in the collection? Some other way? Commented Jun 29, 2016 at 19:41

4 Answers 4

2

You'll have to create a function that loops through your array and checks if all the statuses are 'ALIVE'. Or just use a reduce method on the array:

$scope.allStatusesAreAlive = $scope.myObject.life_log.reduce(function(a, b) {
    if (a === false) return false;
    if (b.status === 'DEAD') return false;
    return true;
}, true);

Then you can display your element if $scope.allStatusesAreAlive is true:

<div ng-if="allStatusesAreAlive"> My element</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome. I tried using a function but myobject is always empty. I'm guessing becasue the view is built before. How do I fix that?
If you're making an AJAX request to get the data for myObject, then you'll need to tell angular to update the view. You can do that by putting your code inside a $timeout (that triggers the digest cycle that rerenders the view. $timeout calls $rootScope.$digest, which triggers the digest cycle). You'll have to inject the $timeout service in your controller and use it like this $timeout(function() { update myObject here });
Also filtering for 'ALIVE' and comparing the length would work and I think that's easier to read. Please have a look at this fiddle. The reduce is also a nice way of detecting it. I think your code can be optimized a bit. Please have a look at the fiddle.
1

You can do something like this

angular.forEach($scope.myObject.life_log, function(item){
    if(item.status !=='ALIVE'){
        $scope.isAlive = false;
        break;
    }else{
        $scope.isAlive = true;
    }
});

then in your html

<div data-ng-if="isAlive">My element</div>

Comments

0

Check the following link

https://plnkr.co/edit/ermeKk1dBX8D54pL7vHQ?p=preview

angular code:

  $scope.myObject = {'life_log' : [{status: 'ALIVE'},{status: 'DEAD'}]}
             $scope.val=true;
              for(i=0;i<$scope.myObject.life_log.length;i++){

                if($scope.myObject.life_log[i].status != "ALIVE")
                $scope.val=false;
              }

html code:

<div ng-show="val">My element</div>

Comments

-1
// This is here to make it configurable
$scope.keys = Object.keys($scope.myObject);

// In this case you have the "life_log" key
$scope.keys.forEach(k => {
   $scope.myObject[k].forEach((d) => {
      // our object here is myObject["life_log"];
      // obj is a temp array to check the amount of statuses that are dead 
      var obj = [];
      d["allAlive"] = d.status.forEach( s =>{
         if(s == 'DEAD'){
            obj.push("Dead");
         }
      });
      if(obj.length > 0){
         d.allAlive = false
      }else{
         d.allAlive = true;
      }
   });
});

   <div ng-if="myObject[o].allAlive>
      ALL ALIVE
   </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.