AngularJS 在 API 回调后恢复表单
AngularJS Restes Form after API Callback
在我的代码中,我有一个带有 ng.resource:
的工厂
.factory('company', function($resource){
return $resource(appHelper.apiPath('auth/company/info'), {}, {
update: {
method: "PUT"
}
});
});
如果我在控制器中提交表单,只要 api 给出肯定响应,一切正常。
如果出现错误,api returns 带有 http 200 的 json 对象。在我的回调函数中,我验证响应:
$scope.saveCompanyForm = function (company) {
company.$update(
function(data) {
if(data.status == 'ERROR') {
alert("error from api")
} else {
alert("no error")
}
}, function(error) {
alert("error")
}
问题是 api returns 表单清除时出现错误。
如果 API 使用 http 500 或 http 404 响应,则表单未被清除。
是否有可能阻止 angular 重置表格?
谢谢
最佳
回调前随时保存,回调后申请
$scope.saveCompanyForm = function (company) {
var saved_company = company;
company.$update(
function(data) {
if(data.status == 'ERROR') {
alert("error from api")
company = saved_company;
} else {
alert("no error")
company = saved_company;
}
}, function(error) {
alert("error")
company = saved_company;
}
在我的代码中,我有一个带有 ng.resource:
的工厂.factory('company', function($resource){
return $resource(appHelper.apiPath('auth/company/info'), {}, {
update: {
method: "PUT"
}
});
});
如果我在控制器中提交表单,只要 api 给出肯定响应,一切正常。 如果出现错误,api returns 带有 http 200 的 json 对象。在我的回调函数中,我验证响应:
$scope.saveCompanyForm = function (company) {
company.$update(
function(data) {
if(data.status == 'ERROR') {
alert("error from api")
} else {
alert("no error")
}
}, function(error) {
alert("error")
}
问题是 api returns 表单清除时出现错误。 如果 API 使用 http 500 或 http 404 响应,则表单未被清除。 是否有可能阻止 angular 重置表格? 谢谢 最佳
回调前随时保存,回调后申请
$scope.saveCompanyForm = function (company) {
var saved_company = company;
company.$update(
function(data) {
if(data.status == 'ERROR') {
alert("error from api")
company = saved_company;
} else {
alert("no error")
company = saved_company;
}
}, function(error) {
alert("error")
company = saved_company;
}