2017-08-31 33 views
2

我使用角JS 1.6setPristine和setUntouched沒有工作

<div flex="100" flex-gt-sm="50"> 
     <md-input-container class="md-block"> <label>Conduct 
      Code</label> <md-select name="ConductCode" ng-model="conductcode" 
      style="min-width: 200px;" required> <md-option 
      ng-repeat="val in olCentreFromOtherConduct.conduct" ng-value="val.acadamiccalanderid" ng-bind="val.condcutcode"></md-option> 
     </md-select> 
     <div ng-messages="CentreFromOtherConductForm.ConductCode.$error"> 
      <div ng-message="required">Please Select Conduct Code</div> 
     </div> 
     </md-input-container> 
</div> 

當我復位值

$scope.Reset = function() { 
     $scope.conductcode = 0; 
} 

然後其狀態應非觸摸!!我曾嘗試不同的東西以下,但沒有什麼工作

//   $scope.CentreFromOtherConductForm.ConductCode.$touched = true; 
//   $scope.CentreFromOtherConductForm.ConductCode.$valid = true; 
//   $scope.CentreFromOtherConductForm.ConductCode.$invalid = false; 
//   $scope.CentreFromOtherConductForm.ConductCode.$setUntouched(); 

OR

angular.forEach($scope.CentreFromOtherConductForm, function (input) { 
      if (input && input.hasOwnProperty('$viewValue')) { 
       input.$setPristine(); 
       input.$setUntouched(); 
      } 
}); 

任何更新!

+1

是ConductCode形式的名字嗎?它看起來不是。另外,$ setPristine()在表單上不是用於輸入。 – rrd

+0

'CentreFromOtherConductForm'表單名稱和'ConductCode'是元素名稱'md-select name =「ConductCode」'@rrd –

回答

1

答案是調用窗體上的方法,而不是在輸入:

$scope.CentreFromOtherConductForm.$setPristine(); 
$scope.CentreFromOtherConductForm.$setUntouched();