是否有可能在http错误响应后将mat输入设置为错误状态?在从下面的代码提交表单时,im发送http请求,我想显示mat错误,但它只显示输入处于错误状态时,我不知道如何在错误响应后将其设置为"manualy“。
<form [formGroup]="exampleForm" (ngSubmit)="onSubmit()">
<mat-form-field>
<input matInput placeholder="placeholder" formControlName="Input" required>
<mat-error *ngIf="variable">error message</mat-error>
</mat-form-field>
</form>
发布于 2019-02-08 12:50:28
serviceClass:
export class serviceClass{
private url: string = 'your url'// your http request url
constructor(private http: HttpClient) { }
callApi() {
return this.http.get(this.url);
}
}componentClass:在订阅http请求时,如果在响应中出现错误,可以将errorVariable设置为true。
export class componentClass{
private url: string = 'your url'
public errorVariable:boolean;
constructor(private service: serviceClass) { }
getApi() {
return this.service.callApi().subscribe(
(response:string)=>{
console.log(response);
},
(error)=>{
console.error(error);
this.errorVariable=true;
}
);
}
}htmlTemplate:
<mat-error *ngIf="errorVariable">error message</mat-error>https://stackoverflow.com/questions/54592661
复制相似问题