我有一个非常简单的表单,其中包含几个输入字段。这是一个:
<label class="item item-input item-stacked-label">
<span class="input-label">First Name</span>
<input type="text" ng-model="signInData.firstName" placeholder="">
</label>
当我提交表单并且它从AJAX请求返回true时,我只想清空输入字段,但是它不起作用。我可能缺少一些简单的方法。
这是我要清空的控制器代码:
$scope.signInData.firstName = '';
这是从范围中清空模型,我可以看到这是因为我正在控制台上记录范围对象,但是它并没有清空值。
我已经用Google搜索了一段时间,并看到了诸如$ setPristine方法之类的东西,但是这些都不起作用。
编辑
$http({
url: 'sign_in_app',
method: 'POST',
data: {'firstName': signInData.firstName}
}).then(function(response) {
if(response.data.response == 'error'){
$scope.errorSignIn = 'Sorry, there was an error signing in.';
}else{
$scope.errorSignIn = null;
$scope.signInData.firstName = '';
$scope.signInForm = null;
}
}
编辑-表格
<form ng-submit="signIn(signInData)" id="signInForm" name="signInForm">
<div ng-bind-html="errorSignIn" class="center error"></div>
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">First Name</span>
<input type="text" ng-model="signInData.firstName" placeholder="">
</label>
<label class="item">
<button class="button button-full button" type="submit">Sign in</button>
</label>
</div>
</form>
编辑3-更新的HTTP调用
$http({
url: '/sign_in/sign_in_app',
method: 'POST',
data: {'firstName': $scope.signInData.firstName, 'lastName': $scope.signInData.lastName}
}).then(function(response) {
if(response.data.response == 'error'){
$scope.errorSignIn = 'Sorry, there was an error signing in.';
}else{
$scope.errorSignIn = null;
$scope.signInData.firstName = '';
$scope.signInForm = null;
}
}
尝试以下操作:signInData
从中删除ng-submit
,更改您的$scope.signIn()
方法以使其不再需要,使用该$scope.signInData
对象来设置您的$http
调用的所有值(顺便说一句,该值可能应该在服务中,以使您的控制器更加整洁并遵循常规角度设计模式),然后查看是否仍然存在无法清除表单输入的问题。
我的怀疑是,因为从那时signInData
起您就开始对作用域对象的副本进行操作,因此,清除属性的行为不像您期望的那样。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句