Forms 表单字段错误,即使在表单设置为pristine:AngularJs后也未重置

Forms 表单字段错误,即使在表单设置为pristine:AngularJs后也未重置,forms,angularjs,validation,Forms,Angularjs,Validation,在我的HTML中: <form class="form_page login-frm" name="loginForm" novalidate> <input type="email" ng-model="data.username" name='username' required autofocus> <div class="info-message"> <small ng-show='(loginForm.username.$

在我的HTML中:

<form class="form_page login-frm" name="loginForm" novalidate>
   <input type="email" ng-model="data.username" name='username' required autofocus>
   <div class="info-message">
     <small ng-show='(loginForm.username.$error.required || loginForm.username.$error.email) && loginForm.username.$dirty'>The acceptable format for the username includes an '@'.
     </small>
   </div>
   <button class="btn btn-primary" ng-click="cancel()">Cancel</button>
</form>

但在此之后,如果用户名不正确,显示错误消息并单击“取消”按钮,则错误消息将被删除,但用户名字段永远不会清空。如何同时清空用户名。以及在删除值后,为什么它会可见

$scope.data.username
设置为空字符串或未定义,而不是将其删除。您也不需要控制器中带有
angular.element(…)
的行。

我创建了plunkr来回答您的问题。 您应该以更面向对象的方式来实现这一点

var User = function(username) {
    this.username = username;
};
User.prototype.reset = function() {
    this.username = "";
};
$scope.user = new User("Ali");
$scope.cancel = function() {
    $scope.message = "Resetting form";
    $scope.userForm.$setPristine();
    $scope.user.reset();
};

$scope.data.username=''?删除该值不是更好的方法。你看,我还有其他字段,如果thoseNo没有错误,我会删除这些工作。该模型是一个有角度的应用程序的中心,如果你开始从中切割出大块,那么你将受到影响。向数据对象添加一个名为reset的函数,并将所有值设置为null、空字符串或0。EUREKA:如果表单字段出错,则ng模型不会更新。删除值不是更好的方法吗。你看,我也有其他字段,如果这些字段没有错误,那么删除这些字段意味着属性不存在,这意味着JavaScript将搜索原型链。对于表单,这很少是您想要的。在我看来,将其设置为空或未定义的值更干净。
var User = function(username) {
    this.username = username;
};
User.prototype.reset = function() {
    this.username = "";
};
$scope.user = new User("Ali");
$scope.cancel = function() {
    $scope.message = "Resetting form";
    $scope.userForm.$setPristine();
    $scope.user.reset();
};