Javascript AngularJS未正确处理按钮类型=";重置";?

Javascript AngularJS未正确处理按钮类型=";重置";?,javascript,forms,angularjs,validation,Javascript,Forms,Angularjs,Validation,我有以下资料: <form name="editor"> <h2>Create/Update</h2> {{ editor.title.$error.required }} <div ng-class="{ 'has-error': editor.title.$invalid && editor.title.$dirty, 'has-success': editor.title.$valid }" class="f

我有以下资料:

<form name="editor">
    <h2>Create/Update</h2>
    {{ editor.title.$error.required }}
    <div ng-class="{ 'has-error': editor.title.$invalid && editor.title.$dirty, 'has-success': editor.title.$valid }" class="form-group">
        <input type="text" name="title" ng-model="project.title" placeholder="title" required class="form-control">
        <span class="input-icon fui-check-inverted" ng-show="editor.title.$valid"></span>
    </div>
    {{ editor.description.$error.required }}
    <div ng-class="{ 'has-error': editor.description.$invalid && editor.description.$dirty, 'has-success': editor.description.$valid }" class="form-group">
        <textarea name="description" ng-model="project.description" placeholder="description" required class="form-control"></textarea>
        <span class="input-icon fui-check-inverted" ng-show="editor.description.$valid"></span>
    </div>
    <button ng-click="save()" type="submit" class="btn btn-primary btn-embossed">Save</button>
    <button type="reset" class="btn btn-inverse">Reset</button>
    <a href="#/">Back</a>
</form>

创建/更新
{{editor.title.$error.required}}
{{editor.description.$error.required}}
拯救
重置
我遵循上的“JavaScript项目”教程。我在detail.html中添加了重置按钮。问题在于它的行为。当我自己清理字段时,验证失败,但当我单击重置按钮时,字段正在清理,但表单仍然有效。
那是一只角虫吗?如何修复它呢?

我相信您正在寻找的是
$setPristine()

$setPristine()
将窗体设置为原始状态

可以调用此方法删除“ng dirty”类并将窗体设置为其原始状态(ng pristine类)。此方法还将传播到此表单中包含的所有控件

当我们希望在保存或重置表单后“重用”表单时,将表单设置回原始状态通常很有用

通过

示例JS 示例标记
新plunk:为什么要重置?我从来都不明白原因,也从来没有遇到过我需要的情况。有一半的时候我不小心撞到了他们,把表格清空了。你真的需要吗?@Phix,不,我不需要,但这种行为似乎很奇怪。嗯,是的。你不能认为这是一种传统的形式;不是。它与外部模型相关联。如果你想让它知道任何可能的变化,你需要让你的控制器知道。如果您对控制器中的
reset()
函数不感兴趣,我已经在上面的更新中简化了解决方案。实际上,这对我没有帮助。我的表单实际上是动态绑定到用户在列表中选择的实际对象。因此,我要实现的是,当用户编辑数据,然后改变主意时,可以点击重置,然后恢复原始值。仍然在寻找如何以一种简单的方式实现这一点,而不必重复使用诸如纪念品之类的东西。@IgorDonin-您可以将用户附加值存储到
localStorage
。然后,重置函数将执行以下操作:
$scope.data.username=localStorage.getItem('username')| |“”
$scope.data = {
    "username": "", 
    "password" : ""
};

$scope.reset = function() {
  $scope.data.username = "";
  $scope.data.password = "";
  $scope.form.$setPristine();
};
<form name="form" id="form" novalidate>
        <input name="username" ng-model="data.username" placeholder="Name" required/>
        <input name="password" ng-model="data.password" placeholder="Password" type="password" required/>
        <div>
            <hr />
            <button class="button" ng-click="">Login</button>
            <button class="button" ng-click="reset()">Reset</button>
        </div>
</form>
<button class="button" type="reset" ng-click="form.$setPristine()">Reset</button>