簡體   English   中英

如何在前端調用后端錯誤消息?

[英]How to call backend error message on the frontend?

在我的 Angular 項目中,我在將錯誤消息從后端返回到前端時遇到問題。 目標是在值 msisdn 不符合續訂條件時顯示錯誤消息。 現在,每當插入值時,錯誤消息“MSISDN 不符合續訂條件”都是硬編碼的,假設它是 isEligible = false 就太籠統了。

 initForm(): void { this.renewForm = this.fb.group({ deductAmt: [], planType: [], msisdn: [ '', { validators: [ Validators.required, Validators.pattern(AppConstant.PHONE_REGEX) ], asyncValidators: [], updateOn: 'blur' } ], expiryDate: [], }); } onBack(): void { this.router.navigate([RouteConstant.POSTLOGIN, RouteConstant.DASHBOARD]); } proceed(): void { validateFormFields(this.renewForm); if (this.renewFormValid) { return; } this.renewPlan(); }
 <input matInput placeholder="Enter MSISDN" [formControl]="msisdn" required/> <mat-error *ngIf="msisdn.dirty && msisdn.hasError('required')">This field is required.</mat-error> <mat-error *ngIf="msisdn.dirty && msisdn.hasError('pattern')">MSISDN not valid.</mat-error> iv class="row" *ngIf=".isBar"> <div class="col-lg-12"> <label class="text-danger"> <span>MSISDN is not eligible for renewal.</span> </label> </div>

當您收到來自 api 然后 append 與您的前端的消息的響應時,您需要在后端設置錯誤消息

例如,您設置如下響應

let respons={
data:resData,
message:"Some message for success"
}

如果出現錯誤,那么如果您想發送數據或不發送數據,您需要進行如下設置!

let respons={
    data:resData,
    message:"MSISDN is not eligible for renewal."
    }

現在在前端

假設你有錯誤

那么你需要簡單的response.message

現在它會在前端動態顯示什么消息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM