【发布时间】:2016-04-02 01:21:28
【问题描述】:
HTML:
<form name="ContactForm" novalidate >
<div class="list">
<label class="item item-input">
<input type="text" placeholder="First Name" ng-model="contact.firstName" name="uFirstName" required="" autocomplete="off"/>
</label>
<br>
<div ng-show="ContactForm.$submitted || ContactForm.uFirstName.$touched" ng-hide="hidespan">
<span class="error" ng-show="ContactForm.uFirstName.$error.required">Tell us your First Name.</span>
<span class="error" ng-show="ContactForm.uFirstName.$error.text">This is not a valid First Name.</span>
</div>
</div>
<button class="button button-block button-assertive" ng-click="ContactForm.$valid && contactReq(contact)">
Submit
</button>
</form>
JS
var defaultForm = {
firstName : "",
lastName : "",
email : "",
message : ""
}
$scope.contact = angular.copy(defaultForm);
$scope.contactReq = function(contact){
$scope.ContactForm.$setPristine();
$scope.contact = angular.copy(defaultForm);
console.log('empty');
}
我正在使用 AngularJS v1.4.3 进行 phonegap 开发。单击提交按钮后,我想重置表单。但是当我设置 Pristine 时,我收到以下错误:“错误:错误:未定义不是对象(正在评估 '$scope.ContactForm.$setPristine')” 我尝试了许多相关的链接,但没有任何东西对我有用。
非常感谢任何帮助。
【问题讨论】:
-
我认为 ContactForm 没有在控制器范围内定义
-
@AlainIb 第一名,他为什么要在控制器中定义
ContactForm? -
发布一个 complete 最小示例来重现该问题。我的猜测是您的表单位于
ng-if或另一个具有自己范围的指令中。 -
你好@Jose ..是的,我已经解决了这个问题......通过添加 $scope.untouched = false; $scope.ContactForm.$setUntouched();.....提交后我也在刷新我的页面。
标签: angularjs