【发布时间】:2016-12-26 19:53:24
【问题描述】:
你好惊人的 Stackoverflow 编码器,请问如何在表单提交后清空 Angular Js 中的表单输入。
下面是我的工作代码
$rootScope.sendNow = function () {
if ($rootScope.send_login.username.length < 1)
alert("Please enter username");
else if ($rootScope.send_login.password.length < 1)
alert("Please enter password");
else {
$http.post($rootScope.server_url + "post_Data", $rootScope.send_login)
.success(function (response)
{
// on success
$('#send_login.username').val('');
$('#send_login.password').val('');
toastr.success('Successful');
}
).error(function (response)
{
alert("sending failed");
});
}
};
我的表格在下面
<input type="text" id="username" name="username" ng-value="send_login.username">
<input type="text" id="pass" name="pass" ng-value="send_login.password">
<input type="button" ng-click="sendNow()" value="Send">
与 Ajax 不同,我在下面尝试过,但无法让它工作
$('#send_login.username').val('');
$('#send_login.password').val('');
【问题讨论】:
-
使用 ng-model 将您的输入绑定到范围变量字段,并将此变量设置为 {}。
-
使用角度必须使用@jb-nizet 提到的范围
-
Angularjs form reset的可能重复