user500468
user500468

Reputation: 1221

Pass object into ng-model in selectbox

I have an selectbox that is filled with data from my backend. The data that comes from my backend Is an array with objects:

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

I use ng-options to make the options In the select:

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

As you can see, I capture the the selected option with onChangeSuperCustomer:

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

The thing I want Is to pass the whole object that belongs to the selected option, so I can access all the properties that belongs to It. How can I accomplish this?

Upvotes: 0

Views: 1144

Answers (2)

Anita
Anita

Reputation: 2400

Simply by passing 'item' in 'onChangeSuperCustomer' method

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

Upvotes: 0

eladcon
eladcon

Reputation: 5825

change your ng-options to bind the entire object item, instead of just 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>

Upvotes: 1

Related Questions