Load data in $ scope dynamically

2

I have a call to an API, in which I recover several data.

I just want to show some of the fields, so I have created an array with the fields I want to show:

var CamposAPI = ['a','b','c','d','e','f'];
var campo = CamposAPI[i]; (está dentro de un for, según los campos que contenga CamposAPI)

Which I use to then show it with the following line:

$scope.campo = response.data["Clientes"][0][campo];

If I make a console.log the data is seen correctly, but when they are stored, they do not reach the view.

I'm sure I'm doing something wrong in the $ scope.field part but I do not see the one.

I add the code of the view:

<md-card layout-gt-sm="row" layout-padding  ng-controller="MyCtrl">
  <table class="table">
    <tbody>
      <tr>
        <td class="text-left"><strong>a</strong></td>
        <td class="text-left" colspan="5">{{a}}</td>
      </tr>
      <tr>
        <td class="text-left"><strong>v</strong></td>
        <td class="text-left">{{b}}</td>
        <td class="text-left"><strong>c</strong></td>
        <td class="text-left" colspan="3">{{c}}</td>
      </tr>
   </tbody>
</mdcard>

Greetings.

    
asked by Diego 10.10.2016 в 17:54
source

1 answer

2

In this case I think you should do something like that.

<tbody>
  <tr>
    <td class="text-left"><strong>a</strong></td>
    <td class="text-left" colspan="5">{{campo[0]}}</td>
  </tr>
  <tr>
    <td class="text-left"><strong>v</strong></td>
    <td class="text-left">{{campo[1]}}</td>
    <td class="text-left"><strong>c</strong></td>
    <td class="text-left" colspan="3">{{campo[2]}}</td>
  </tr>

You already have the matrix then the effort of assigning a data in $ scope for each one is not necessary. Put the matrix in $ scope and access them by the matrix.

    
answered by 10.10.2016 / 21:29
source