gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:53:31 24 4
gpt4 key购买 nike

我正在创建一个带有 ng-options 的选择,它循环遍历链接到 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 开头的 address 中添加 .idng-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.id as address.address + ' ' + address.address2 + ' ' + address.city + ', ' + address.state + ' ' + address.zipcode for address in addresses" ></select>

最佳答案

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

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

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

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

关于javascript - 带有链接到 ng-init 的对象数组的 ng-options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45083061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com