gpt4 book ai didi

javascript - AngularJS - 如何在选择中预先选择值?

转载 作者:行者123 更新时间:2023-11-28 19:33:54 25 4
gpt4 key购买 nike

我有一系列可用的选择项目:

// Default available date formats
$scope.dateformats = [
{
code: 'YY.MM.DD',
name: 'YY.MM.DD'
},
{
code: 'DD.MM.YY',
name: 'DD.MM.YY'
}
];

我正在尝试像这样默认预选值:

$scope.actualDateformat = $scope.dateformats[0].code;
<select ng-options="dateformat.name for dateformat in dateformats" ng-model="actualDateformat" ng-change="changeDateFormat(dateformat)">
<option style="display:none" value="">{{actualDateformat}}</option>
</select>

问题是,“预选”值作为第一个选项标 checkout 现在列表中>

<option style="display:none" value="">{{actualDateformat}}</option>

从剩余的两个动态添加的项目中选择任何一个后,第一个选项中的文本会附加所选项目的文本(和值)。

请问如何解决?

我想要这样的结果:

<select>
<option value="YY.MM.DD">YY.MM.DD</option>
<option value="DD.MM.YY" selected>DD.MM.YY</option>
</select>

感谢您的帮助。

最佳答案

这里是FIDDLE

您的问题是您正在选择整个对象而不是该对象的代码字段。

dateformat.name for dateformat in dateFormats
label : dateformat.name
object : dateformat

dateformat.code as dateformat.name for dateformat in dateformats
label : dateformat.name
object : dateformat.code

此外,我不明白 optiondisplay:none 的需要。

您可以像这样选择dateformat.code

<select ng-options="dateformat.code as dateformat.name for dateformat in dateformats" ng-model="actualDateformat" ng-change="changeDateFormat(dateformat)">
</select>

关于javascript - AngularJS - 如何在选择中预先选择值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26251807/

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