2017-08-24 41 views
0

我有一個表單,當我提交時,我會在表單提交後重新初始化它。然後我顯示一條消息並保持在同一頁面上。ng-Submit提交併抑制ng消息後重置表格

但是,表單的字段在窗體被「觸摸」時會顯示錯誤消息。

以下證明:

enter image description here

我看了一下如何去解決這個的一些文章,但沒有在爲我工作。

我的HTML:

<form name="newPost" ng-submit="makeNewPost()"> 
<div class="form-group"> 
    <input name="title" maxlength="46" minlength="2" type="text" class="form-control" ng-model="post.title" required="required"> 
    <div ng-messages="newPost.title.$error" ng-if="newPost.title.$touched"> 
    <div class="errorMessage" ng-message="required">Title is mandatory *</div> 
    </div> 
</div> 

<input type="submit" value="Submit" class="btn btn-success" id="submit"> 

我的控制器代碼重置數據:

var resetData = function(){ 
    $scope.post = {}; 
}; 

resetData(); 

當然還有更多的領域,但要解決這個問題,就這個簡單的代碼會證明它。

任何輸入都會有所幫助。謝謝你!

回答

1

你resetData功能應該是:

$scope.resetData = function(){ 
    $scope.post = {}; 
    $scope.newPost.$setUntouched(); 
    $scope.newPost.$setPristine(); 
} 

其中newPost是窗體名稱& $ setUntouched,$ setPristine會使原始的形式,就像最初加載。在提交函數結束時調用該函數。

+0

你是一個傳奇!非常感謝。像魅力和一半的作品 –

+1

@ShayanKhan很高興幫助:) – Shantanu