带有链接到 ng-sun 的对象数组的 ng-options

ng-options with array of objects linking to an ng-init

我正在创建一个带有 ng-options 的 select,它循环遍历链接到 ng-init 的对象数组。但是,问题是,它还必须通过 ng-change 传递正确的变量。

现在下面的代码将使 ng-init 工作并在加载时选择正确的选项。 However, when the options are selected it doesn't 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>

通过在 ng-options 的开头将 .id 添加到 addressng-change 可以完美运行,但它会使 select 下拉列表为空负载。

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

地址[0] 将是这一行中的整个地址对象:

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

为了只保留id,我觉得你应该改成:

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