Ng-options with an array of ng-init related objects

I am creating a select with ng-options

that traverses an array of related objects ng-init

. However, the problem is that it has to pass the correct variable through ng-change

.

For now, the code below will do the ng-init

work and select the desired option at boot. However, when the options are selected, it does not pass the ID tong-change

<select name="baddress" ng-init="autoship.baddress_id = address.id || addresses[0]" 
ng-model="autoship.baddress_id" 
ng-change="setter('baddress', autoship, address.id)" 
ng-options="address as address.address + ' ' + address.address2 + ' ' + address.city + ', ' + address.state + ' ' + address.zipcode for address in addresses" ></select>

      

Adding .id

to address

the beginning ng-options

, ng-change

it works fine, but it leaves the selected picture is blank on startup.

<select name="baddress" 
ng-init="autoship.baddress_id = address.id || addresses[0]" 
ng-model="autoship.baddress_id" 
ng-change="setter('baddress', autoship, address.id)" 
ng-options="address.id as address.address + ' ' + address.address2 + ' ' + address.city + ', ' + address.state + ' ' + address.zipcode for address in addresses" ></select>

      

+3


source to share


1 answer


adresses [0] will be the entire addressable object on this line:

ng-init="autoship.baddress_id = address.id || addresses[0]" 

      



To keep just the ID, I think you should change it to:

ng-init="autoship.baddress_id = address.id || addresses[0].id" 

      

0


source







All Articles