1

my object:

objects = [
{
    name: "1",
    foo: [{
      value: 0.5,
      ignored: null,
      text: "foo1"
    }, {
      value: 0,
      ignored: null,
      text: "foo"
    }]
},
{
    name: "2",
    foo: [{
      value: 0,
      ignored: null,
      text: "foo"
    }, {
      value: 0.5,
      ignored: null,
      text: "foo2"
    }]
},
{
    name: "3",
    foo: [{
      value: 0.5,
      ignored: null,
      text: "foo3"
    }]
},
{
    name: "4",
    foo: [{
      value: 0,
      ignored: 1,
      text: "foo4"
    }]
},
{
    name: "5",
    foo: [{
      value: 0,
      ignored: null,
      text: "foo5"
    }]
}]

my AngularJs template html:

<div class="row" ng-repeat="object in objects">
  {{ object.name }}
  <div ng-repeat="foo in object.foo">
    <div ng-if="foo.value > 0 || foo.ignored == 1">
      {{ foo.text }}
    </div>
  </div>
</div>

My result is:

1 foo1
2 foo2
3 foo3
4 foo4
5

but i do not want display object.name for case N°5.

How to use 2nd ng-repeat and ng-if in 1st ng-repeat please?

<div class="row" ng-repeat="object in objects" ng-if="...???...">
  {{ object.name }}
  any text !!
</div>
6
  • Your array is not an array of arrays but an array of objects. Could you please clarify what you mean by "with different properties"? Does that mean that the elements in objects can have different structures? Commented Feb 28, 2019 at 15:43
  • @FlorianLim, sorry, I re edit my post Commented Feb 28, 2019 at 15:48
  • So why don't you just put ng-if="object.foo.value > 0 || object.foo.ignored == 1" on the main ngRepeat directive? Commented Feb 28, 2019 at 15:48
  • @AlonEitan, foo is an array Commented Feb 28, 2019 at 15:51
  • @sgrillon Oh, I didn't see that edit. Can foo contain more than one object, or would it always contain a single object? Commented Feb 28, 2019 at 15:54

2 Answers 2

1

You can use a filter function in ng-if.

Changed template:

<div class="row" ng-repeat="object in objects" ng-if="myFilter(object)">
  {{ object.name }}
  <div ng-repeat="foo in object.foo">
    <div ng-if="foo.value > 0 || foo.ignored == 1">
      {{ foo.text }}
    </div>
  </div>
</div>

Filter function in your controller:

$scope.myFilter = function(o) {
  // Returns a truthy value if the foo-subarray meets the criteria.
  var found = o.foo.find(function(fooElement) {
    return fooElement.value > 0 || fooElement.ignored == 1;
  });
  return found;
};

Result:

1
foo1
2
foo2
3
foo3
4
foo4 
Sign up to request clarification or add additional context in comments.

Comments

0

You could simply do:

<div class="row" ng-repeat="object in objects">
  <div ng-repeat="foo in object.foo">
    <div ng-if="foo.value > 0 || foo.ignored == 1">
      {{ object.name }}
      {{ foo.text }}
    </div>
  </div>
</div>

If you know in advance that only one element of object.foo can ever pass the ngIf. (Like is the case in your example)

1 Comment

sorry but in your solution object.name display several times.

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.