2
我試圖在AngularJS中使用$setPristine
函數來重置文本框,但它似乎不會導致所需的行爲。
我的形式如下:
<form name="addInviteForm" ng-controller="InviteCtrl" ng-submit="sendInvitation(userEmail)">
Pristine? {{addInviteForm.$pristine}}
<!-- email input -->
<div>
<input type="email" name="email" ng-model="userEmail" placeholder="Enter email here" class="line-item-input see" required>
<span class="error" ng-show="addInviteForm.email.$error.email" style="color:red">Invalid Email</span>
</div>
<!-- submit button -->
<input type="submit" name="send" class="btn btn-success center" value="Send Invitation">
</form>
而且在我的控制器對應的代碼:
$scope.sendInvitation = function(userEmail) {
// do some work here ...
// hmm, this doesn't seem to work ...
$scope.addInviteForm.$setPristine();
};
雖然形式顯示$pristine
是在形式項設置爲true
,然後設置爲false
當在文本框中輸入數據時,在提交表單後確實顯示$pristine
設置爲true ....但文本框中的值與提交按鈕之前的值保持一致被按下。
我在這裏錯過了什麼?
謝謝。將'userEmail'綁定到'InviteCtrl'範圍內的一個變量並且在提交時重置該變量看起來已經成功了。 – dtg
儘管我會說,這些文檔根本沒有說得那麼清楚。也就是說,控制的「狀態」和控制的當前「價值」之間的語義區別並不明顯.... – dtg