2017-03-15 113 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>