gpt4 book ai didi

javascript - 如何在angularjs中选择默认下拉选项?

转载 作者:行者123 更新时间:2023-11-30 16:17:14 24 4
gpt4 key购买 nike

如何在 angularjs 中选择下拉列表?

如何为“Third Project 456”选择默认下拉选项

请检查:jsfiddle

HTML:

<select ng-change="getMileWorkerlist(broadcastManagerData.project_list)" ng-model="broadcastManagerData.project_list" name="project_list" class="form-control" required >
<option value="0">All Project</option>
<option ng-repeat="(key, value) in projectList" ng-value="key">{{value}}</option>
</select>

JavaScript:

$scope.projectList = {
35: "Third Project 456",
56: "Project-11-1-2016",
76: "test project for cbs"
};
$scope.broadcastManagerData.project_list =35;

在查看源代码中制作我的 html:

<select class="form-control ng-pristine ng-valid ng-valid-required ng-touched" required="" name="project_list" ng-model="broadcastManagerData.project_list" ng-change="getMileWorkerlist(broadcastManagerData.project_list)">
<option value="? number:35 ?"></option>
<option ng-selected="true" ng-value="0" value="0" selected="selected">All Project</option>
<option class="ng-binding ng-scope" ng-value="key" ng-repeat="(key, value) in projectList" value="35">Third Project 456</option>
<option class="ng-binding ng-scope" ng-value="key" ng-repeat="(key, value) in projectList" value="56">Project-11-1-2016</option>
<option class="ng-binding ng-scope" ng-value="key" ng-repeat="(key, value) in projectList" value="76">test project for cbs</option>
</select>

输出:

enter image description here

请检查:jsfiddle

最佳答案

好吧,我知道为什么我的评论不起作用,我假设(没有仔细阅读问题)你正在使用数组,但你不是。我不知道如何使用(键,值)来做到这一点,但你为什么不使用数组?如果你是,这会起作用:

<select ng-change="getMileWorkerlist(broadcastManagerData.project_list)"
ng-model="broadcastManagerData.project_list"
ng-init="broadcastManagerData.project_list = projectList[0].key"
name="project_list" class="form-control" required
ng-options="item.key as item.value for item in projectList">
</select>

在 Controller 中,数组:

$scope.projectList = [
{key: 35, value: "Third Project 456"},
{key: 56, value: "Project-11-1-2016"},
{key: 76, value: "test project for cbs"}
];

但正如我所说,这种方式似乎只适用于数组。为什么不能这样做?

关于javascript - 如何在angularjs中选择默认下拉选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35308414/

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