Niko Lang
Niko Lang

Reputation: 559

How to check for data type in ng-repeat

I have a ng-repeat and two possible types of tags for each data type (string or object). At the moment both tags are being rendered.

<div ng-app="myApp" ng-controller="myCtrl" ng-init="init()">
    <div ng-repeat="(key,value) in json">
        <span>{{value}}</span><!-- Only for strings and numbers -->
        <table border="1"><!-- Only for objects -->
            <tr ng-repeat="(k,val) in value"><td>{{k}}</td><td>{{val}}</td></tr>
        </table>    
    </div>
</div>

Fiddle

Upvotes: 2

Views: 1764

Answers (2)

Bleja_
Bleja_

Reputation: 61

<table border="1" ng-if="angular.isObject(yourVarabile)">
This may work..

Upvotes: 0

IggY
IggY

Reputation: 3125

Please find bellow a working jsFiddle

https://jsfiddle.net/vy984q9a/

ng-show and ng-hide let you easily show or hide an element based on a condition

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope){
    $scope.init = function(){
        $scope.json = {
            "fold":11,
            "id":64894760,
            "entities":{"bezeichnung":"Basis","name":"Basis","id":16}
        }   
    }
    $scope.isObject = function(value) {
        return angular.isObject(value);
    }

});
</script>

<div ng-app="myApp" ng-controller="myCtrl" ng-init="init()">
    <div ng-repeat="(key,value) in json">
        <span ng-hide="isObject(value)">{{value}}</span><!-- Only for strings and numbers -->
        <table ng-show="isObject(value)" border="1"><!-- Only for objects -->
            <tr ng-repeat="(k,val) in value"><td>{{k}}</td><td>{{val}}</td></tr>
        </table>    
    </div>
</div>

Upvotes: 3

Related Questions