Angularjs 使用ng模型时从控制器清空表单输入字段
我有一个非常简单的表单,有几个输入字段。这里有一个:Angularjs 使用ng模型时从控制器清空表单输入字段,angularjs,input,angularjs-scope,angular-ngmodel,Angularjs,Input,Angularjs Scope,Angular Ngmodel,我有一个非常简单的表单,有几个输入字段。这里有一个: <label class="item item-input item-stacked-label"> <span class="input-label">First Name</span> <input type="text" ng-model="signInData.firstName" placeholder=
<label class="item item-input item-stacked-label">
<span class="input-label">First Name</span>
<input type="text" ng-model="signInData.firstName" placeholder="">
</label>
这是从作用域清空模型,我可以看到,因为我正在控制台记录作用域对象,但它只是没有清空值
我在谷歌上搜索了一段时间,看到了$setPristine方法等,但这些都不起作用
编辑
编辑-表格
<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>
$setPristine
不会清理输入字段。你可以尝试使用
$scope.signinanda={}
提交表单后尝试以下操作:从ng submit
中删除signinada
,将$scope.signIn()
方法更改为不再需要它,使用$scope.signinada
对象设置$http
调用的所有值(顺便说一句,这可能是为了让你的控制器更干净,并遵循典型的角度设计模式)然后看看你是否仍然存在表单输入未被清除的问题
我的怀疑是,因为您从此点开始传递
signenda
,所以您开始对scope对象的副本进行操作,这就是为什么清除属性的行为与您期望的不一样。在重置模型的位置显示完整的代码上下文为什么在提交时传递signenda
,您要做什么正在处理该对象?是否可能正在以某种方式创建卷影副本?您不能像那样传递signInData。您必须像$scope.signInData那样传递。firstName@JamesG尝试以下操作:从ng submit中删除signInData,将signIn scope方法更改为不再需要该方法,使用$scope.signInData对象设置$h的所有值ttp调用(顺便说一句,它应该在一个服务中,以保持控制器更干净,并遵循典型的角度设计模式),然后查看是否仍然存在表单输入未被清除的问题。@JamesG是角度控制器中的$http()调用吗?
$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>
$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;
}
}