2015-04-22 77 views
3

我设置了一个表单来验证服务器端的OnBlur事件。服务器端验证工作正常,并按预期返回错误。但是,一旦将字段的有效性设置为false,将其设置回true不会消除$错误消息。是不是$ setValidity true应该从表单中删除错误?

这是控制器:

angular.module('artists').controller('ArtistsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Artists', 
    function($scope, $stateParams, $location, Authentication, Artists) { 
    $scope.authentication = Authentication; 

    $scope.processForm = function(val){ 

     var artist = new Artists({ 
      name: $scope.artistForm.name.$viewValue, 
      quote: $scope.artistForm.quote.$viewValue 
     }); 
     artist.$save(function(response) { 
      $scope.artistForm.$setValidity(val,true); 
     }, function(errorResponse) { 
      if(val in errorResponse.data){ 
      $scope.artistForm.$setValidity(val,false,errorResponse.data[val].message); 
      }else{ 
      $scope.artistForm.$setValidity(val,true); 
      } 
     }); 

    }; 
}]); 
+1

我想'$ scope.artistForm $ setPristine()'会帮助你 –

+0

setPristine是关系到“肮脏“或”未触动状态“的形式。它与错误验证不同。请参阅:http://stackoverflow.com/questions/17947639/removing-field-errors-when-setting-form-to-pristine-state –

+0

什么是你的函数中的'val'? –

回答

0

在我的情况的字段设置恢复正​​常,但是当我编辑。发生这种情况是因为$ scope从不同的控制器加载并且不适用。因此,你需要使用$apply

$scope.$apply(function(){ 
    $scope.artistForm.$setValidity(val,true); 
}) 

这里的文档: https://docs.angularjs.org/api/ng/type/ $ rootScope.Scope