0

I have a little problem with creating a dynamic HTML template. To display some data in a table, I have to separate between an ordinary String and an Array. First things first: The HTML template is included in another template using the data-ng-include directive with a controller. The controller contains my data which is essentially an object with different attributes. The objcect looks something like this and contains strings as well as arrays:

$scope.data = {
    name: 'tony',
    city: 'New York',
    friends: ['Ann', 'Ben', 'John Doe'],
    postal: 12345
};

There is also a matching array with the attributes to parse the information dynamically.

$scope.attributes = [{name: "Name", id: 'name'},
                {name: "City", id: 'city'},
                {name: "Friends", id: 'friends'},
                {name: "Postal-code", id: 'postal'}
                ];

The data gets parsed inside the HTML using this code:

<table class="table table-striped">        
    <tbody>
      <tr ng-repeat="attr in attributes">
        <td>{{attr.name}}</td>
        <td>{{data[attr.id]}}</td>
      </tr>
    </tbody>
  </table>

This works absolutly fine for strings but I have to somehow determine if a attribute of data is an array to display the single records in a kind of List using ng-repeat on the record.

I tried to use Array.isArray(data[attr.id]) with an if-clause in different variations inside of <script>-tags but nothing works. At the end the data of the friends-attribute should get displayed one below the other in the same cell like this:

enter image description here

Here is the code on JsFiddle

2
  • Can you elaborate? Commented Aug 11, 2016 at 9:16
  • Something like this? All values are transformed to array and then iterated over. Commented Aug 11, 2016 at 9:17

3 Answers 3

1

you can isArray but not directly inside ng-show.

you can use it like this.

 $scope.isArray = angular.isArray;

so this way,

 <tr ng-repeat="person in persons">
   <td ng-repeat="attr in query.attribs">
     <span ng-show="isArray(person[attr.id])">
       <span ng-repeat="p in person[attr.id]">
          {{p}}
       </span>
     </span>
     <span ng-show="!isArray(person[attr.id])">
       {{person[attr.id]}}
     </span>
   </td>
</tr>

Here is your updated Fiddle

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

2 Comments

This works great for me. I just had to add track by $index to the ng-repeat ="p in person[attr.id] directive
ok. you got what you need :) if its working then please accept answer
1

You have to do some script and html changes if the value is an array like this Updated fiddle here : jsfiddle.net/Lt024p9h/7/

Comments

0

Do Something Like this.

<tr ng-repeat="person in persons">
   <td ng-repeat="attr in query.attribs">
     <span ng-show="isArray(person[attr.id])">
       {{person[attr.id].toString()}}  
     </span>
     <span ng-show="!isArray(person[attr.id])">
       {{person[attr.id]}}
     </span>
   </td>
</tr>  

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.