Aro Tonoyan Aro Tonoyan -4 years ago 96
AngularJS Question

Inspect all items on ng-repeat

If the first prematchGame has 1 and 2 value ,after that add header , but in this case i have some issue ,,,first element hasn't 1 and 2 value , but 3th and 4th has it . (see on image)

How can i check , that if 1 element has 1 and 2 value in ng-repeat then add in header .

My code
(and sorry for bad english)

<ul>
<li ng-show="groupGames[0].firstMarket.events.P1" title="1">1</li>
<li ng-show="groupGames[0].firstMarket.events.P2" title="2">2</li>
<ul>

<div ng-repeat="prematchGame in groupGames track by prematchGame.id">
...
</div>


enter image description here

Answer Source

define a function to filter for the target item.

$scope.hasItemWithProperty = function (key) {
    return $scope.groupGames.some(prematchGame => {
        return prematchGame.firstMarket.events.hasOwnProperty(key);
    });
}

use the function in the template like this

<ul>
  <li ng-show="hasItemWithProperty('P1')" title="1">1</li>
  <li ng-show="hasItemWithProperty('P2')" title="2">2</li>
<ul>

this is a plunker of the above.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download