gpt4 book ai didi

javascript - ng-model 不与 select 一起使用

转载 作者:行者123 更新时间:2023-11-28 15:12:27 24 4
gpt4 key购买 nike

这可能是一个重复的问题,但在将另一个数组绑定(bind)到下拉列表时显示数据的默认值时我陷入困境。列表已填充,但加载后应显示的选定值并未显示。

这是我的示例代码:

HTML:

<div ng-app="app">
<div ng-controller="myCntrl">
<select class="form-control right"
ng-if="subscription.subscribedAddresses.ChannelType=='EMAIL'"
ng-model="subscription.subscribedAddresses.ChannelAddress"
ng-options="channel.ChannelAddress for channel in commInfo.emails track by channel.ChannelAddress" required>
</select>
</div>

JS:

<小时/>
angular.module('app', [])
.controller('myCntrl', ['$scope', function($scope) {
$scope.commInfo = {
"emails": [{
"ChannelAddressId": 5000054652,
"DeliveryChannel": "EMAIL",
"ChannelType": null,
"ChannelAddress": "ya_ajay@net.com"
}, {
"ChannelAddressId": 5000075277,
"DeliveryChannel": "EMAIL",
"ChannelType": null,
"ChannelAddress": "yad_ay@ts.com"
}, {
"ChannelAddressId": 5000075278,
"DeliveryChannel": "EMAIL",
"ChannelType": null,
"ChannelAddress": "yadav_aaj@gmail.com"
}, {
"ChannelAddressId": 5000075279,
"DeliveryChannel": "EMAIL",
"ChannelType": "UNKNOWN",
"ChannelAddress": "test_ay@mail.com"
}],
"phones": [{
"ChannelAddressId": 5000075390,
"DeliveryChannel": "PHON",
"ChannelType": "UNKNOWN",
"ChannelAddress": "4561237895"
}, {
"ChannelAddressId": 5000075397,
"DeliveryChannel": "PHON",
"ChannelType": "UNKNOWN",
"ChannelAddress": "7894561236"
}]
};
$scope.subscription = {"serviceName":"RAM Month","subscribedAddresses":{"ChannelAddress":"yad_ajay@ts.com","ChannelType":"EMAIL"}};
}]);

这是 Plunkr: Plunkr: Dropdown issue - Angularjs

最佳答案

代码对我来说似乎很好,但是您将 subscription.subscribeAddresses.ChannelAddress 初始化为“yad_ajay@ts.com”。我在您的 commInfo 中没有看到任何 yad_ajay@ts.com。我猜这就是问题所在。

编辑:这是您的plunker的工作选择

 <select class="form-control right" 
ng-if="subscription.subscribedAddresses.DeliveryChannel=='EMAIL'"
ng-model="subscription.subscribedAddresses"
ng-options="channel as channel.ChannelAddress for channel in commInfo.emails track by channel.ChannelAddressId"
required>
</select>

更改:将 ng-if 中的 ChannelType 更改为 DeliveryCHannel

更改 ng-options add track by 以帮助 Angular 识别对象的唯一性

工作中的笨蛋:- http://plnkr.co/edit/LSbJ2RVRw9zJleSwP3YT?p=preview

关于javascript - ng-model 不与 select 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35791628/

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