如果object属性是另一个对象,则隐藏角度

时间:2016-05-25 16:16:03

标签: javascript angularjs

在我的ngController中,我遍历一个可以是值或对象的对象属性。 我的对象

   _{
    "_id":"12545",
    "createdBy":{"_id":"someid","name":"somename"},
    "createdOn":"2016-05-13T14:34:47.322Z",
    "item":{"id":"1235561","name":"track"},
    "players":[{"_id":"someid","name":"somename"}],
    "maxPlayers":4,
    "minPlayers":1,
     "state":"open"}

循环我使用的属性

<div ng-repeat="(key, value) in game">
   <b>{{key}}</b> - {{value}}
 </div>

现在我只想显示属性值而不是对象。 所以我希望显示_id,_createdOn,maxPlayers,minPlayers,州但 createdBy,item,players。

如何检查它是否是一个对象然后隐藏它,我试过:

<div ng-repeat="(key, value) in game" ng-hide="isObject(value)">
    <b>{{key}}</b> - {{value}}
</div>

-

<div ng-repeat="(key, value) in game" ng-hide="{{isObject(value)}}">
    <b>{{key}}</b> - {{value}}
</div>

也尝试过:

 value.isObject();

但它没有用。

3 个答案:

答案 0 :(得分:3)

尝试使用:

$scope.isObject = function(obj) {
    return (typeof obj === 'object');
  };

实施例:https://jsfiddle.net/ofL9zvuL/1/

答案 1 :(得分:0)

您始终可以将ng-hide从ng-repeated元素移开:

<div ng-repeat="(key, value) in data">
  <div ng-hide='isObject(value)'>
    <b>{{key}}</b> - {{value}}
  </div>
</div>

$scope.isObject = function(value) {
    if (typeof value === 'object') {
       return true;
    }

    return false;
};

答案 2 :(得分:0)

我这里有一个有效的解决方案。我正在使用ng-show。它在ng-repeat div上工作。

Fiddle

  <div ng-repeat="(key, value) in game" ng-show="isObject(value)">
      <b>{{key}}</b> - {{value}}
  </div>

  $scope.game = {
"_id": "12545",
"createdBy": {
  "_id": "someid",
  "name": "somename"
},
"createdOn": "2016-05-13T14:34:47.322Z",
"item": {
  "id": "1235561",
  "name": "track"
},
"players": [{
  "_id": "someid",
  "name": "somename"
}],
"maxPlayers": 4,
"minPlayers": 1,
"state": "open"
};



$scope.isObject = function(value) {
    return typeof value == 'object';
  };