-1

你好我有同样的问题

选择问题

我有这样奇怪的选择

<option value="? string:98 ?"></option>

html代码

 <select  ng-model="vm.student" class="form-control">
   <option value="0">All</option>
   <option data-ng-repeat="src in vm.studentList" value="{{ src.Id }}"> {{ 
   src.Value }} </option>
</select>

并且底部的默认值没有出现我正在尝试很多方法来解决这个问题,但不幸的是,没有办法很好地工作

我希望能帮助我解决这个问题

谢谢

4

2 回答 2

0

可以使用 ng-options 解决

通过参考 Madhan Varadhodiyil 示例

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="AppCtrl">
       <select  ng-model="student" class="form-control" ng-options="src.Id as src.Value for src in studentList">
         <option value="">All</option>   <!-- value="" helps to remove void space from dropdown -->
       </select>                
    </div>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('AppCtrl', function($scope){
    $scope.studentList = [
        {
            "Id": 98,
            "Value": "Jon Snow"
        },
        {
            "Id": 99,
            "Value": "Mother of dragons"
        }
    ];
    
   $scope.student = $scope.studentList[0].Id;	// I want Id:98 to be selected by default
});</script>

希望它有帮助,干杯!

于 2018-08-03T11:44:55.517 回答
0

使用ng-value指令:

<select ng-model="vm.student" class="form-control">
   <option ng-value="0">All</option>
   <option data-ng-repeat="src in vm.studentList" ng-value="src.Id">
     {{src.Value}}
   </option>
</select>

了解更多信息,

于 2018-08-03T11:11:00.990 回答