Mikel Granero
Mikel Granero

Reputation: 419

AngularJS dynamic select inside ng-repeat

I'm printing a list of products in a table. This list of products has a product type (typeId). I want to be able to change the typeId of the product directly in the table by using a select input.

The problem comes when I try to set the default option of the product in the select input, because It shoud be the option with the typeId.

ng-repeat="x in screensList". The x represents a product.

I tried using ng-init or ng-value, but it won't work. What value should be in the ng-model of each select?

Code and images:

<tbody>
    <tr ng-repeat="x in screensList">
      <th scope="row">
        {{x.pantalla}}
      </th>
      <td>
        {{x.description}}
      </td>
      <td>
         <select class="form-control" ng-model="slideshowSelect" ng-options="y.title for y in slideshowsList track by y.id">
         </select>
      </td>
      <td class="text-right">
         <button type="button" ng-click="deleteService($event)" serviceId="{{x.id}}" class="btn btn-danger btn-sm">Eliminar</button>
      </td>
    </tr>
</tbody>

Colored code: enter image description here

Result: enter image description here

Upvotes: 0

Views: 152

Answers (1)

Bill P
Bill P

Reputation: 3618

I think this is what you need:

 <select class="form-control" ng-model="x.idSlideshow" ng-options="y.id as y.title for y in slideshowsList">
 </select>

Check the working example: fiddle link

Upvotes: 1

Related Questions