0

So I have an html element like this:

 <div ng-hide="!myarr.length">
    Im showing
 </div>

Somewhere else an array is populated.
Before it is being populated it will not have a length, so the div above will not show up.

However, when the array will be filled with data (json objects) I need to understand if all the objects in the array contain a specific field (and it cannot be empty).

So my json could eventually look like this:

myarr[0] = {name: 'jack', type: ''}
myarr[1] = {name: 'jack'}
myarr[2] = {name: 'jack', type: 'mytype'}

In the case above, the div should continue to be hiding as not all the json objects contain a "type"-field that is not empty.

How do I accomplish this? I was thinking to have an OR statement in the ng-hide. Maybe I could use the "some"-function. But when doing this it seems I cannot run this kind of function inside the ng-hide (at least my editor marks the code with red). Is there an alterntive way to do this without needing to call a function in my controller?

1 Answer 1

2

When you retrieve the data, you can iterate over each object in the array and examine the individual objects. If any individual object DOES NOT contain the necessary data, set the display flag to false and break out of the loop.

$scope.showElement = true;
$scope.array = [
    {name: 'jack', type: ''},
    {name: 'jack'}
    {name: 'jack', type: 'mytype'}
];

for(let i=0; i<$scope.array.length; i++){
  if($scope.array[i].type === undefined || $scope.array[i].type === ''){
    $scope.showElement = false;
    break;
  }
}
Sign up to request clarification or add additional context in comments.

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.