角$ setPristine()不工作 [英] Angular $setPristine() not working

查看:394
本文介绍了角$ setPristine()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用角的内置函数的形式,特别是 setPristine()来清除用户提交表单。我的控制器与它的所有方法访问 $ scope.newForm (我的表单),但运行 $ scope.newForm。$ setPristine()未重置表单域。

I'm trying to use Angular's built-in form functions, specifically setPristine() to clear the form on user submit. My controller has access to $scope.newForm (my form) with all of its methods, but running $scope.newForm.$setPristine() isn't resetting the form fields.

下面是我的HTML:

<div ng-controller="NewFormController">
    <h3>New Entry</h3>

    <form name="newForm" method="post" novalidate>
        <div class="input-group">
            <label>Name</label>
            <input name="name" type="text" ng-model="place.name"/>
        </div>
        <div class="input-group">
            <label>Description</label>
           <textarea name="description" type="text" ng-model="place.description"></textarea>
        </div>
        <div class="input-group">
           <label>Neighborhood</label>
           <input name="neighborhood" type="text" ng-model="place.neighborhood"/>
        </div>
        <div class="input-group">    
            <label>Address</label> 
           <input name="location" type="text" ng-model="place.address"/>
        </div>
        <input type="submit" value="Submit" ng-click="submit(place)"/>
    </form>
</div>

和这里就是我所说的控制器 setPristine()

And here is the controller where I call setPristine():

app.controller('NewFormController', function($scope, $compile) {

    $scope.place = { 
        name: 'ExamplePlace', 
        description: 'This is a description!', 
        neighborhood: 'Manhattan', 
        address: '112 Street Place' 
    };

    $scope.submit = function(place) {
        $scope.newForm.$setPristine();
        $scope.newForm.$setUntouched();
    };

});

这里是一个工作codePEN 重现我的问题。

请注意:我使用角版本1.4.3

Note: I'm using Angular version 1.4.3.

推荐答案

$ setPristine 仅标志着形式是 $质朴,这对于验证驱动前pressions和CSS是非常有用的(如 .ng脏

$setPristine only marks the form as being $pristine, which is useful for validation-driven expressions and CSS (e.g. .ng-dirty)

因此​​, $ setPristine 不清除窗体的控件。事实上,它甚至不知道的如何的做到这一点。想想,要清除可能意味着不同的事情,不同的型号。 清除可能意味着未定义,或任何东西,一个自定义的输入控件与 ngModel 可能意味着

So, $setPristine does not clear the form's controls. In fact, it wouldn't even know how to do that. Consider, that to "clear" could mean different things to different models. "Clear" could mean "", or undefined, or null, or anything at all that a custom input control that works with ngModel could mean.

因此​​,要正确清除表单是修改驱动形式的清除,它需要什么定义视图模型。在大多数情况下 - 包括你 - 它只是一个视图模型设置为一个新对象的事:

So, to properly clear the form is to modify the View Model that drives the form to whatever definition of "clear" it needs. In most cases - yours included - it is just a matter of setting the View Model to a new object:

$scope.submit = function(place) {
   $scope.newForm.$setPristine();
   $scope.newForm.$setUntouched();

   // clear the form
   $scope.place = {};
};

这篇关于角$ setPristine()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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