将对象传递到选择框中的ngmodel

时间:2015-04-14 12:09:44

标签: javascript angularjs

我有一个填充了后端数据的选择框。来自我后端的数据是一个包含对象的数组:

 [Object { superkund_id="4", nod_id="12068", namn="Växjö Fagrabäck"}, Object { superkund_id="5", nod_id="9548", namn="Halmstad Bågen / Pilen"}]

我使用ng-options制作选项在选择中:

<td><select class="form-control input-sm2" ng-model="selectedSupercustomer" ng-options="item.superkund_id as item.namn for item in superkundOptions" ng-change="onChangeSuperCustomer(selectedSupercustomer)" ><option value=''>Select</option></select></td>

如您所见,我使用onChangeSuperCustomer捕获所选选项:

 $scope.onChangeSuperCustomer = function(selectedSupercustomer) {
           //console.log(selectedSupercustomer); 
        }

我想要的是传递属于所选选项的整个对象,因此我可以访问属于它的所有属性。我怎么能做到这一点?

2 个答案:

答案 0 :(得分:1)

更改ng-options以绑定整个对象item,而不仅仅是superkund_id

<td><select class="form-control input-sm2" ng-model="selectedSupercustomer" ng-options="item as item.namn for item in superkundOptions" ng-change="onChangeSuperCustomer(selectedSupercustomer)" ><option value=''>Select</option></select></td>

答案 1 :(得分:0)

只需在'onChangeSuperCustomer'方法中传递'item'

即可
<select class="form-control input-sm2" ng-model="selectedSupercustomer" ng-options="item.superkund_id as item.namn for item in superkundOptions"
     ng-change="onChangeSuperCustomer(item)" >
 <option value=''>Select</option>
</select>