Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a dictionary like this:

var data = {
  a: [1, 2, 3, 4, 5],
  b: [
    [1, 2],
    [3, 4],
    [5, 6]
  ]
};

Now I need to use ng-hide to hide an element if 2 exists in data->a. It would be done easily like this:

<i ng-hide="(data.a | filter:2).length">2 not found</i>

Now how I can do it with data->b? I need to hide the message if 2 is found in any of data->b items.

I found How do I only show an element if nested ng-repeat is not empty?. But I need to show to message only once.

share|improve this question
    
just using ng-hide="(data.b | filter:2).length" works for me –  New Dev Jan 16 at 2:04
    
@NewDev Probably ng-show. I need to show to message only once. [if not empty] ? confusing.. :/ –  PSL Jan 16 at 2:06
    
@PSL I need to put the code @New Dev suggested inside a loop. As 2 of b's elements doesn't contain 2, the 2 not found message will be shown twice. I need to hide the message, only once, if 2 appears in any of b's lists. –  AliBZ Jan 16 at 2:49

2 Answers 2

up vote 1 down vote accepted

You need to create one filter that will accept array. And it will give you ouput on basis of provided value.

HTML

<div ng-hide="(data.b | filteronsubarray:2)">2 not found</div>

Filter Code

app.filter('filteronsubarray', function() {
  return function(arr, toFind) {
    var isFound = false;
    angular.forEach(arr, function(val, index) {
      angular.forEach(val, function(v, i) {
        if(v == toFind){
          isFound = true;
        }
      });
    });
    return isFound;
  }
});

Here is Fiddle link(http://plnkr.co/edit/gApW4sAYMOBLPh8gJyE3?p=preview)

Hope this is helpful to you.

share|improve this answer
    
Mark the answer if it is helpful to you. –  pankajparkar Jan 20 at 5:34
    
Thats what I was looking for. Thanks. –  AliBZ Jan 20 at 19:27
1  
PS. I will mark it as the correct answer. Be patient!! –  AliBZ Jan 20 at 19:27
    
@AliBZ Thanks. Glad to help you :) –  pankajparkar Jan 20 at 19:34

If all you need is to do whatever you did for data.a, except for each element of data.b, then you just need to iterate over data.b and apply the same logic:

<i ng-repeat="b in data.b"
   ng-hide="(b | filter:2).length">2 not found</i>

Plunker

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.