我假设api返回一个500响应,其中包含那些模型错误?我会这样做:
validationErrors = [];
this.myService.save(employee).subscribe(response => {
// The first parameter will be from any 200 codes, do what you want on success
},
err => {
// The second parameter will be from a 500 code, your error object
console.log(err); // Check out what the object looks like, usually what I want is in err.error;
this.validationErrors = [];
for (var errorArray in err.error.ModelState) {
if (err.error.ModelState.hasOwnProperty(errorArray )) {
this.validationErrors = this.validationErrors.concat(errorArray);
}
}
}