Ashish Jat
Ashish Jat

Reputation: 1

how to check that array inside an array has any value (angularjs)

There is an array contentMetaDataList inside another array (content) which contents few field like title, name, etc. so how i can hide heading (smart tags: written inside p tag) when there is no title to display.

Smart Tags :

{{relatedcontent.title}}

Upvotes: 0

Views: 42

Answers (2)

zb22
zb22

Reputation: 3231

You can use ng-show in order to show/hide by condition, and to check if all the objects in your array includes the title field, you can use Array.every().

If you comment or remove one of those titles in the array, the wrapper <div> which show the titles won't be visible, but stays in the DOM tree. (removing from the DOM tree can be done by ng-if)

var app = angular.module('myApp', []);
app.controller('MainCtrl', ['$scope','$http', function($scope, $http){
$scope.a = 'sdf';
  
  $scope.contentMetaDataList = [
    {
      title:"legislation_title",
      fieldValue:"refund of tax to certain persons",
      id:94346
    }, 
    
    {
      title:"Enterprise_title",
      fieldValue:"refund of tax to certain persons",
      id:94346
    },
    
    {
      title:"Related_title",
      fieldValue:"refund of tax to certain persons",
      id:94346
    }];
  
   $scope.checkTitle = function() {
      return $scope.contentMetaDataList.every(item => item.title);
   }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.3/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="MainCtrl">
        <div>
          
          <div ng-show="checkTitle()">
            <p>Smart Tags : </p> 
            <div ng-repeat="relatedcontent in contentMetaDataList">
                  <div>
                      {{relatedcontent.title}}
                  </div>
             </div>
          </div>
        </div>
  </div>
</div>

for more info:

ng-show
Array.every()
ng-if

Upvotes: 1

Rene Trujillo
Rene Trujillo

Reputation: 389

I would say add a <span> or some tag with a ngIf. *ngIf="relatedcontent.title !== undefined"

Upvotes: 0

Related Questions