ng-selected不使用ng-options,但使用ng-repeat工作正常

时间:2017-12-18 10:28:33

标签: javascript angularjs

我使用ng-selectedng-valueng-options。但我无法选择值ng-selected无法正常工作

  <select  name="team_selected" 
    required="" ng-model="edit_team_name" ng-change="edit_selectTeam(edit_team_name)" 
    ng-selected='edit_team_id === team.team_id' ng-value="team.team_id" 
    ng-options="team.team_name for team in teamNameAvailable track by team.team_id">                                
  </select>

2 个答案:

答案 0 :(得分:0)

<select  name="team_selected" required="" ng-model="edit_team_name" ng-change="edit_selectTeam(edit_team_name)" >   
<option ng-repeat="team.team_name for team in teamNameAvailable track by team.team_id"  ng-selected='edit_team_id === team.team_id'  ng-value="team.team_id"></option                             
 </select>

答案 1 :(得分:0)

在您的情况下,我在选择框

中创建了一个默认值

而不是edit_team_id === team.team_id这个ng-model本身将选择选择值表格选项

&#13;
&#13;
var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
$scope.teamNameAvailable=[{
                              team_id:1,
                              team_name:"USA"
                          },{
                              team_id:2,
                              team_name:"India"
                          },{
                              team_id:3,
                              team_name:"Aus"
                         }];

$scope.edit_team_name=$scope.teamNameAvailable[1];
});
&#13;
select{
width:150;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp" ng-controller="testCtrl">
<select  name="team_selected" 
    required="" ng-model="edit_team_name" ng-change="edit_selectTeam(edit_team_name)" 
    ng-options="team.team_name for team in teamNameAvailable track by team.team_id">                                
  </select>
  </div>
&#13;
&#13;
&#13;