Search code examples
javascripthtmlangularjsangularjs-ng-repeat

ng-repeat in html table


I am making an html table and I use this code:

<table>
               <thead>
                   <tr>
                       <th colspan="2">The table header</th>
                   </tr>
               </thead>
               <tbody>
                  <tr ng-repeat="item in myDynamicLabels">
                    <td> {{item}}</td>
                   </tr>
                   <tr ng-repeat="value in myDynamicData track by $index" >
                       <td>{{value}}</td>
                   </tr>
              </tbody>
           </table>

The output i get is item item item value value value

But i want it to be in columns so: item value item value item value

How can i fix this and still use ng-repeat?

   angular.forEach(data.results.bindings, function(val)
       {
           $scope.myDynamicLabels.push(val.state.value);
           $scope.myDynamicData.push(val.nbr_university.value);

       })

This is my javascript code for the labels and data


Solution

  • Modify your code like this:

     angular.forEach(data.results.bindings, function(val)
           {
               let jsonData = {
                 state: val.state.value,
                 university: val.nbr_university.value
               }
               $scope.myDynamicData.push(jsonData);
    
           })
    

    And use ng-repeat something like this:

    <table>
       <thead>
          <tr>
            <th colspan="2">The table header</th>
          </tr>
       </thead>
       <tbody>
          <tr ng-repeat="value in myDynamicData track by $index" >
            <td>{{value.state}}</td>
            <td>{{value.university}}</td>
          </tr>
       </tbody>
    </table>
    

    Hope this is what you are willing to do.