如何在同一个select中绑定两个变量的数据

时间:2016-11-09 12:06:33

标签: javascript html angularjs

我这里有这个代码:

<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>                     

事情是......正如我正在做的那样

  

vm.service.type = serviceType.name

,我也想做

  

vm.service.serviceTypeId = serviceType.id

...我如何仅使用此选择字段?

1 个答案:

答案 0 :(得分:0)

看看下面的例子。

<div ng-app="myApp" ng-controller="myCtrl">

<p>Select a car:</p>

<select ng-model="selectedCar" ng-options="x for (x, y) in cars">
</select>

<h1>You selected: {{selectedCar.brand}}</h1>
<h2>Model: {{selectedCar.model}}</h2>
<h3>Color: {{selectedCar.color}}</h3>

<p>Note that the selected value represents an object.</p>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.cars = {
        car01 : {brand : "Ford", model : "Mustang", color : "red"},
        car02 : {brand : "Fiat", model : "500", color : "white"},
        car03 : {brand : "Volvo", model : "XC90", color : "black"}
    }
});
</script>

在此处找到它 - http://www.w3schools.com/angular/tryit.asp?filename=try_ng_select_object_object