Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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 提交时出现表格错误_Javascript_Angularjs_Forms_Angular Material_Mean Stack - Fatal编程技术网

Javascript 提交时出现表格错误

Javascript 提交时出现表格错误,javascript,angularjs,forms,angular-material,mean-stack,Javascript,Angularjs,Forms,Angular Material,Mean Stack,我有一个表格,其中有一个必填字段和一个非必填字段。成功提交表单并将数据发送到服务器后,我将清除两个输入字段模型中的数据。但是,这样做时,必填字段将变为红色,并显示错误此字段为必填字段。提交表单并清除其数据后,如何保持必填字段无错误。我希望它仅在用户尝试提交带有空必填字段的表单时显示错误 <form name="myForm"> <div layout="row" layout-align="center center" flex="100"> &l

我有一个表格,其中有一个必填字段和一个非必填字段。成功提交表单并将数据发送到服务器后,我将清除两个输入字段模型中的数据。但是,这样做时,必填字段将变为红色,并显示错误
此字段为必填字段
。提交表单并清除其数据后,如何保持必填字段无错误。我希望它仅在用户尝试提交带有空必填字段的表单时显示错误

<form name="myForm">
    <div layout="row" layout-align="center center" flex="100">
        <md-input-container class="md-block" flex="40">
            <input required type="text" placeholder="Book Name"
                   ng-model="bookName"
            />

            <div ng-messages="$error">
                <div ng-message="required">This is required.</div>
            </div>
        </md-input-container>

        <span flex="10"></span>

        <md-input-container class="md-block" flex="40">
            <input type="text" placeholder="Theme"
                   ng-model="theme"
                   enter-pressed=""/>
        </md-input-container>
    </div>
</form>

<md-button class="md-raised md-primary" flex="none" type="submit"
           ng-click="onSubmitClicked()">Submit
</md-button>


    $scope.onSubmitClicked = function() {
        $scope.bookName = ""; // this causes the REQUIRED FIELD ERROR AFTER I SUBMIT THE FORM
        $scope.theme  = "";
    };

这是必需的。
提交
$scope.onSubmitClicked=function(){
$scope.bookName=”“;//这会在我提交表单后导致必填字段错误
$scope.theme=“”;
};

除了Asiel的答案,您应该在清除模型后执行以下操作

                 if(form){ //make sure a form is passed as parameter
                     form.$setPristine(); //this will reset the form to its original state
                     form.$setUntouched();
                 }

表单。$setUntouched
将表单设置为未触及状态,从而删除错误消息。

请执行以下操作:

<form name="myForm" ng-submit="onSubmitClicked(myForm)">

  <md-button class="md-raised md-primary" flex="none" type="submit">Submit</md-button>
</form>


$scope.onSubmitClicked = function(form) {
    if (form.$valid) {
          // do what you have to do, the form is validated
        return;
    }

    // dont do anythign the form will flag ans $invalid and $submitted and the error messages will show
    return;

};

提交
$scope.onSubmitClicked=函数(表单){
如果(表格$valid){
//做你必须做的事,表格就会被验证
返回;
}
//不做任何事情表单将标记ans$invalid和$SUPPRED,并显示错误消息
返回;
};