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

时间:2016-02-10 06:35:57

标签: javascript angularjs

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

如何为"第三个项目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

4 个答案:

答案 0 :(得分:1)

<div ng-app="myApp" ng-controller="Ctrl" ng-init="broadcastManagerData.project_list = 'Third Project 456'">
     <select ng-model="broadcastManagerData.project_list"  >
      <option value="0">All Project</option>
      <option ng-repeat="(key, value) in projectList" ng-value="value" ng-selected="value=='Third Project 456'">{{value}}</option>
    </select>
    {{broadcastManagerData.project_list}}
</div>

答案 1 :(得分:1)

嗯,我知道为什么我的评论不起作用,我认为(没有仔细阅读问题)你正在使用数组,而你却没有。我不知道如何使用(键,值),但为什么不使用数组?如果你这样做,这将有效:

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

并在控制器中,数组:

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

但正如我所说,似乎这种方式只适用于数组。你有什么理由不这样做吗?

答案 2 :(得分:0)

请检查以下内容是否有帮助:

<option ng-repeat="(key, value) in projectList" ng-value="key" ng-selected="value == 'Third Project 456'">{{value}}</option>

答案 3 :(得分:0)

Weight: 146 (inches)
Height: 70 (inches)
Result: 20.948603801493316 (BMI)