首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >角集垫-在http错误响应后输入到错误状态

角集垫-在http错误响应后输入到错误状态
EN

Stack Overflow用户
提问于 2019-02-08 12:37:53
回答 1查看 2.9K关注 0票数 1

是否有可能在http错误响应后将mat输入设置为错误状态?在从下面的代码提交表单时,im发送http请求,我想显示mat错误,但它只显示输入处于错误状态时,我不知道如何在错误响应后将其设置为"manualy“。

代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-08 12:50:28

serviceClass:

代码语言:javascript
复制
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。

代码语言:javascript
复制
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:

代码语言:javascript
复制
<mat-error *ngIf="errorVariable">error message</mat-error>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54592661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档