AngularJS如何重置选择选项?

bvn4nwqk  于 2023-01-29  发布在  Angular
关注(0)|答案(1)|浏览(164)
<select ng-model="selectedAssignTraining" ng-change="filterAssignedMembers(selectedAssignTraining)" ng-options="training.code as training.name for training in activeTrainings" class="form-control" ng-required="true">  <option value="" selected>Select Course</option> </select>

选择选项未被重置如何重置?我尝试将selectedAssignTraining设置为“”或具有值select course的第一个数组

afdcj2ne

afdcj2ne1#

null似乎起作用了。

angular.module('myApp', [])
  .controller('myController', function($scope) {
    $scope.activeTrainings = [{ code: 1, name: 'Training 1' }];
    
    $scope.selectedAssignTraining = 1;
    
    $scope.filterAssignedMembers = function () {};
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="myController">
    <select ng-model="selectedAssignTraining" ng-change="filterAssignedMembers(selectedAssignTraining)"
      ng-options="training.code as training.name for training in activeTrainings" class="form-control"
      ng-required="true">
      <option value="" selected>Select Course</option>
    </select>
    <button ng-click="selectedAssignTraining = null">Reset</button>
    Selected: {{selectedAssignTraining}}
  </div>
</div>

相关问题