How to "data-bind" two variables in the same select?

0

I have this code that is doing to me vm.service.type = serviceType.name:

   <div class="form-group ">
        <label for="field_type"><b>Type</b></label>
        <div class="input-icon right">
            <select required class="form-control"
                    id="field_type"
                    name="type"
                    ng-model="vm.service.type"
                    ng-options="serviceType.name as serviceType.name for serviceType in vm.servicetypes">
                <option></option>
            </select>
        </div>
        <div ng-show="vm.service.type==null" class="form-group has-error">
            <p class="help-block" ng-show="vm.service.type==null" data-translate="entity.validation.required">
                This field is required.
            </p>
        </div>
    </div>

How do I get vm.service.serviceTypeId = serviceType.id ... This without adding another field to the form.

Note: These variables already exist in the controller and I already have everything needed to do what I want.

    
asked by anonymous 09.11.2016 / 11:27

2 answers

0

I think only you put in the controller

vm.service.id  = vm.service.type ;

JS will reference the same memory space and any change in type will reflect in id

But if it does not work perfectly you can also use ng-change like this:

<select required class="form-control"
    id="field_type"
    name="type"
    ng-model="vm.service.type"
    ng-change="vm.service.id = vm.service.type"
    ng-options="serviceType.name as serviceType.name for serviceType in vm.servicetypes">
        <option></option>
</select>
    
09.11.2016 / 13:50
0

I would simplify the ng-options field to simplify its reading.

When we have a array made up of objectos , iterating on it item refers to the object, then I chose the property .name for the text of <option> and in ng-change vm.service.serviceTypeId is equal to property .id of item .

<select required class="form-control"
                id="field_type"
                name="type"
                ng-model="vm.service.type"
                ng-options="item.name for item in vm.servicetypes"
                ng-change="vm.service.serviceTypeId=item.id">
</select>
    
12.11.2016 / 02:11