0

In my ngController I loop over an objects propertys which can be a value or an object. My object

   _{
    "_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"}

To loop the propertys I use

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

Now I just want to show the property which are values and not objects. so I want to show _id, _createdOn, maxPlayers, minPlayers, state but not createdBy, item, players.

How can I do a check to see if it is an object and then hide it, I tried:

<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>

also tried:

 value.isObject();

But it didn't work.

3 Answers 3

3

Try using this:

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

Example:https://jsfiddle.net/ofL9zvuL/1/

Sign up to request clarification or add additional context in comments.

2 Comments

How is this not an answer?
That is the answer, not sure what else I have to add. Since Sven used a function name called isObject, I had defined the same function with the solution. I guess there should not be any confusion or require more details to it.
0

You can always move the ng-hide off of the ng-repeated element:

<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;
};

Comments

0

I have a working solution here. I am using ng-show instead. And it is working when on the 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';
  };

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.