在md-select中预先选择值 - AngularJS

时间:2017-09-06 10:26:53

标签: javascript angularjs

以下是我的md-select下拉列表。如何从控制器中预选列表选项?

<md-input-container ng-init="Vacancies()" class="md-block" style="margin:0 !important;">
    <md-select ng-model="Vacancy" placeholder="Select a Vacancy" id="Md-select1" name="vacancy1" ng-change="setCriteria(Vacancy)">
    <md-option ng-value="vac" ng-repeat="vac in vacancylist">{{vac.JobTitle}}</md-option>
</md-select>
{{vacancy.JobTitle}}

1 个答案:

答案 0 :(得分:0)

我知道至少有两种方法可以实现这一目标:

选项1

添加到HTML:ng-selected="options.indexOf(option) == 1"

类似的东西:

<md-select ng-model="selectedOption" md-on-open="mdSelectOnOpen()">          
         <md-option ng-repeat="option in options" ng-value="option" 
                    ng-selected="options.indexOf(option) == 1"
                    >{{option.name}}</md-option>

    </md-select>

选项2

定义md-select ng-model

<md-select ng-model="selectedOption.id" md-on-open="mdSelectOnOpen()">      
     <md-option ng-repeat="option in options" ng-value="option.id"                  
                >{{option.name}}</md-option>     
</md-select>

其中:

$scope.options = [];
for(var i=1;i<=100;i++) {
  $scope.options.push({id: i, name: "option " + i});
}

 $scope.selectedOption = $scope.options[1];

Demo Codepen