Search code examples
javascriptangularjsng-options

Angular 1.x: attribute of an object inside data-ng-repeat is set as a string, not an object


I'm trying to set a value of the option field as a JSON object inside another JSON object eg. catalogue inside attribute.catalogue. The problem is that it is saved as a String which looks like a JSON object.

I think this is somehow related to the limitation of the option value field. Can I somehow adjust my code, so that the value is stored as a JSON object and not as a String?

This is the code:

<div class="col-sm-3">
    <select ng-model="attribute.catalogue" ng-change="showScope()">
        <option data-ng-repeat="catalogue in catalogueObjects"
                value="{{catalogue}}">{{catalogue.name}}</option>
    </select>
</div>

Solution

  • In general html attribute's can only store string value. In angular world ng-options directive does provide an ability to use assign object when select option selected.

    <select ng-model="attribute.catalogue" ng-change="showScope()"
       data-ng-options="catalogue as catalogue.name for catalogue in catalogueObjects">
    </select>
    

    Reference