如何在角度js

时间:2015-05-26 06:15:34

标签: angularjs

这是我的代码

<div class="form-group">
  <label class="control-label">Student Name: <span class="req-field">*</span>
  </label>
  <select ng-change="selectAction()" ng-model="formData.Name" ng-options="value.CompleStuId as value.Name for value in myOptions" class="form-control" required>
    <option value="">-- Select --</option>
  </select>
</div>

请与我分享您的知识。提前致谢

1 个答案:

答案 0 :(得分:2)

如果您希望将Name作为标签和所选值,则需要使用value.Name as value.Name for value in myOptions

ngOptions [docs]

  

用于数组数据源:

select as label for value in array
     

其中:

value: local variable which will refer to each item in the array or each property value of object during iteration.
label: The result of this expression will be the label for <option> element. The expression will most likely refer to the value variable (e.g. value.propertyName).
select: The result of this expression will be bound to the model of the parent

&#13;
&#13;
var app = angular.module('app', []);

app.controller('myController', function($scope) {
  $scope.formData = {};
  $scope.myOptions = [{CompleStuId: 1, Name: 'a'}, {CompleStuId: 2, Name: 'b'}, {CompleStuId: 3, Name: 'c'}];
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<div ng-app='app' ng-controller='myController'>


<div class="form-group">
  <label class="control-label">Student Name: <span class="req-field">*</span>
  </label>
  <select ng-change="selectAction()" ng-model="formData.Name" ng-options="value.Name as value.Name for value in myOptions" class="form-control" required>
    <option value="">-- Select --</option>
  </select>
</div>
  
  {{ formData.Name }}

</div>
&#13;
&#13;
&#13;