如何检查ng-repeat中的数据类型

时间:2015-10-27 13:28:25

标签: javascript angularjs

我为每种数据类型(字符串或对象)提供了ng-repeat和两种可能的标记类型。目前正在渲染两个标签。

<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

2 个答案:

答案 0 :(得分:3)

请找一个工作的jsFiddle

https://jsfiddle.net/vy984q9a/

ng-showng-hide可让您根据条件轻松显示或隐藏元素

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

<table border="1" ng-if="angular.isObject(yourVarabile)">
这可能有用..