清除Angular表单提交中的输入字段? [英] Clear an input field on form submit in Angular?

查看:169
本文介绍了清除Angular表单提交中的输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经收集到$ setPristine应该这样做,但是它不在我的项目中.这是我的表格:

I've gathered that $setPristine is supposed to do this, but it isn't on my project. This is my form:

form(name="new_project", ng-submit="create_project()")
    div.create_wrapper
        input#project_name(required, ng-model="project.name", type="text")
        select#project_language(required, ng-init="project.language='PHP'", ng-model="project.language", ng-options="language for language in languages")
        input.button.tiny.radius(type="submit", value="Create")

还有js:

$scope.create_project = () ->
    project = new projectFactory this.project
    project.$save project, (form_data) ->
        $scope.projects.push form_data
        $scope.new_project.$setPristine()

没有错误,并且将pristine设置为true,但是输入值仍然保留.

There are no errors, and pristine is set to true, but the inputs value remains.

推荐答案

将其留在这里,因为该其他问题的名字很不完整,无法找到该问题的解决方案.

Gonna leave this here, since that other question is poorly named for finding the solution to this problem.

您必须手动清除表单元素的值.

You have to manually clear the values of the form elements.

例如,如果您拥有:

input#project_name(ng-model="project.name", type="text")

要清空它,您可以执行以下操作:

To empty it you could do:

$scope.project = null

在您的控制器中.

这篇关于清除Angular表单提交中的输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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