本文介绍了AngularJS不正确的手柄按钮类型= QUOT;复位"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

 <表格名称=编辑器>
    < H2>创建/更新< / H>
    {{editor.title。$ error.required}}
    < D​​IV纳克级={'有错误:editor.title $无效和放大器;&安培; editor.title $脏,'有-成功:。editor.title $有效}级=形式-group>
        <输入类型=文本名称=标题NG模型=project.title占位符=头衔必修课=表格控>
        <跨度类=输入图标成奎安检查反转NG秀=$ editor.title有效。>< / SPAN>
    < / DIV>
    {{editor.description。$ error.required}}
    < D​​IV纳克级={'有错误:editor.description $无效和放大器;&安培; editor.description $脏,'有-成功:。editor.description $有效}级=形式-group>
        < textarea的名字=描述NG模型=project.description占位符=说明所需的类=表格控>< / textarea的>
        <跨度类=输入图标成奎安检查反转NG秀=$ editor.description有效。>< / SPAN>
    < / DIV>
    <按钮NG点击=保存()类型=提交级=BTN BTN-主要BTN-浮雕>保存< /按钮>
    <按钮式=复位级=BTN BTN-逆>复位< /按钮>
    < A HREF =#/>返回< / A>
< /表及GT;

我遵循角网站主页。我已经添加了复位键detail.html。的问题是它的行为。当我清理我自己的字段,验证失败,但是当我点击复位按钮,字段被清理,但形式剩余有效。
那是一个棱角分明的错误?而如何把它修好?


解决方案

我相信你要找的是什么 $ setPristine();

Example JS

$scope.data = {
    "username": "",
    "password" : ""
};

$scope.reset = function() {
  $scope.data.username = "";
  $scope.data.password = "";
  $scope.form.$setPristine();
};

Example Markup

<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>

Demo http://plnkr.co/edit/xTQLQH8mCCkY0tIX6n8Y?p=preview


Update : simplified solution

If you wish to do away with the controller function reset(), you could set type="reset" as originally done, but still need to $setPristine() on ng-click

<button class="button" type="reset" ng-click="form.$setPristine()">Reset</button>

new plunk: http://plnkr.co/edit/tNKPyyMboAQjeURn6m6W?p=preview

这篇关于AngularJS不正确的手柄按钮类型= QUOT;复位&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 08:37
查看更多