Working on creating a select dropdown using ng-options
to iterate over an array of objects and linking it to ng-init
. However, running into an issue where the correct variable needs to be passed through ng-change
as well.
The current code is set up in a way that ng-init
functions properly and selects the right option when the page loads. But, when options are chosen, it fails to pass the id to the ng-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
in the beginning of the ng-options
resolves the issue with ng-change
, but it results in the select dropdown being empty upon loading.
<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>