AngularJS不正确的手柄按钮类型= QUOT;复位"? [英] AngularJS does not proper handle button type="reset"?

查看:175
本文介绍了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();


  

$ setPristine();


  
  

设定的形式,它的原始状态。


  
  

此方法可以被调用,以删除'NG-脏'类和表单设置为它的原始状态(NG-原始类)。这种方法也将传播到本表格所载的所有控制。


  
  

设置形式返回到原始状态往往是有用的,当我们要保存或重置后'再利用'的形式。


  
  

http://docs.angularjs.org/api/纳克/类型/ form.FormController


例JS

  $ scope.data = {
    用户名: ,
    密码:
};$ scope.reset =功能(){
  $ scope.data.username =;
  $ scope.data.password =;
  。$ scope.form $ setPristine();
};

标记示例

 <表格名称=形式ID =形式的novalidate>
        <输入名称=用户名NG模型=data.username占位符=姓名要求/>
        <输入名称=密码NG模型=data.password占位符=密码类型=所需的密码/>
        < D​​IV>
            <小时/>
            <按钮类=按钮NG点击=>登录和LT; /按钮>
            <按钮类=按钮NG点击=reset()的>复位< /按钮>
        < / DIV>
< /表及GT;

http://plnkr.co/edit/xTQLQH8mCCkY0tIX6n8Y?p=$p $ PVIEW


更新:简化的解决方案

如果您希望与控制器的功能做掉复位(),你可以设置键入=复位如最初完成,但的还是的需要 $ setPristine() NG-点击

 <按钮类=按钮类型=复位NG点击=形式$ setPristine()。>复位< /按钮>

新普拉克: http://plnkr.co/edit/tNKPyyMboAQjeURn6m6W?p= preVIEW

I have the following:

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

I follow "JavaScript Projects" tutorial on angular website main page. I've added reset button to detail.html. The problem is it's behavior. When I cleaning fields by myself, the validation fails, but when I click on the reset button, fields are cleaning up but the form remaining valid. Is that an angular bug? And how get it fixed?

解决方案

I believe what you're looking for is $setPristine();

$setPristine();

Sets the form to its pristine state.

This method can be called to remove the 'ng-dirty' class and set the form to its pristine state (ng-pristine class). This method will also propagate to all the controls contained in this form.

Setting a form back to a pristine state is often useful when we want to 'reuse' a form after saving or resetting it.

via http://docs.angularjs.org/api/ng/type/form.FormController

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;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆