Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度$setPristine()不工作_Javascript_Angularjs - Fatal编程技术网

Javascript 角度$setPristine()不工作

Javascript 角度$setPristine()不工作,javascript,angularjs,Javascript,Angularjs,我试图使用Angular的内置表单函数,特别是setPristine()在用户提交时清除表单。我的控制器可以使用其所有方法访问$scope.newForm(我的表单),但运行$scope.newForm.$setPristine()不会重置表单字段 这是我的HTML: <div ng-controller="NewFormController"> <h3>New Entry</h3> <form name="newForm" metho

我试图使用Angular的内置表单函数,特别是
setPristine()
在用户提交时清除表单。我的控制器可以使用其所有方法访问
$scope.newForm
(我的表单),但运行
$scope.newForm.$setPristine()
不会重置表单字段

这是我的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>
这就产生了我的问题


注意:我使用的是Angular版本1.4.3。

$setPristine
仅将表单标记为
$pristine
,这对于验证驱动的表达式和CSS(例如
.ng dirty
)非常有用

因此,
$setPristine
不会清除表单的控件。事实上,它甚至不知道怎么做。考虑到,“明确”可能意味着不同的东西不同的模型。“Clear”可能表示
,或
未定义的
,或
null
,或任何与
ngModel
一起工作的自定义输入控件可能表示的内容

因此,正确地清除表单就是修改视图模型,以将表单驱动到它所需要的任何“清除”定义。在大多数情况下(包括您的),只需将视图模型设置为新对象:

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

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

你是说,是不是重置了。。。。是的,
$setPristine
并不是要这样做的-它只是将表单标记为
$pristine
。要重置,您需要清除/重置视图模型,在您的情况下:
$scope.place={}
Yes,“不是”。谢谢,我还以为setPristine()也清除了输入。哦,不,它没有。它甚至不知道如何清除,因为有很多方法可以“清除”-可以是
,或者
未定义的
,或者与自定义控件完全不同的方法。因此,为了“清除”,您将视图模型修改为“清除”的任何方式,通常重置为新对象-这就是它应该如何开始的-
$setPristine()
有助于表单验证,但不会影响输入元素中声明的视图模型变量。我不理解$scope.place的含义。你能告诉我这是什么,并解释它到底是什么吗?@Oliver,
$scope.place
是OP问题中定义的模型,它在角度上没有特殊意义。表达式
$scope.place={}
将模型设置为新对象。
$scope.submit = function(place) {
   $scope.newForm.$setPristine();
   $scope.newForm.$setUntouched();

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