2017-03-15 152 views
0

我使用這個jQuery選擇n個子angularjs在NG選項選擇n個子

<select class="perf-select"> 
    <option selected>1</option> 
    <option>2</option> 
    <option>3</option> 
    <option>4</option> 
    <option>5</option> 
</select> 

jQuery的

jQuery('.perf-select>option:eq(2)').prop('selected', true); 

我如何在angularjs NG選項

<select class="perf-select" ng-model="viewProfileCtrl.graphsForm.institution" 
         ng-options="inst.institution.institution_id as inst.institution.name for inst in viewProfileCtrl.perfiosAnalysisData.institutions" 
         ng-change="viewProfileCtrl.setGraphInsti(viewProfileCtrl.graphsForm.institution)"> 
         <option value="" selected>Select a Bank </option> 
        </select> 
+0

初始化模式選擇默認值 –

+0

如何實現這一目標我來自Angularjs新 – Develop

+1

的可能的複製[如何使用ng-option設置select元素的默認值](http://stackoverflow.com/questions/17329495/how-to-use-ng-option-to-set-default-value-of-select-element) –

回答

0
至今爲此

最好的辦法是通過array.indexOf

ng-options="countries.indexOf(country) as country.name for country in countries" 

Working Plunkr

0

如果你想設置n個子作爲默認選項下拉菜單,你必須設置你的NG-模型按你的第n孩子在控制器中 -

$ scope.graphsForm.institution = 3;

這裏的「graphsForm.institution」是你的下拉模式和「3」是您要設置爲默認的鍵(如你的榜樣「institution_id」)的第n個孩子。

可以在控制器檢查這個更好的understanding--

<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<body> 
 

 
    <div ng-app="myApp" ng-controller="datCtrl"> 
 

 
     <select class="perf-select" ng-model="graphsForm.institution" 
 
\t \t \t ng-options="inst.institution_id as inst.name for inst in institutions" 
 
\t \t \t ng-change="setGraphInsti(graphsForm.institution)"> 
 
\t \t \t <option value="">Select Option </option> 
 
\t \t </select> 
 

 
    </div> 
 

 
    <script> 
 
     var app = angular.module('myApp', []); 
 
     app.controller('datCtrl', function ($scope) { 
 
\t \t \t $scope.graphsForm={}; 
 
\t \t \t $scope.graphsForm.institution=3; 
 
      $scope.institutions=[{institution_id:1,name:"oxford"},{institution_id:2,name:"havard"},{institution_id:3,name:"cambrige"}]; 
 
     }); 
 
    </script> 
 

 
    
 

 
</body> 
 

 
</html>