我用ngbAlert表示Angular .如果我的后端API失败,我会显示一个alert .

<ngb-alert class="veri-alert" [type]="alert.type" [dismissible]="true" *ngIf="showErrorAlert">
<span class="icon icon-alert-dark"></span>
<span class="pl-2">{{ alert.message }}</span>
</ngb-alert>

enter image description here

如果API失败,我将设置为showErrorAlertTrue,并按预期显示alert .现在,如果用户点击alert 框中的"x"按钮,alert 将按预期关闭. 现在,如果用户在没有刷新的情况下再次通过单击Continue按钮对后端进行API调用,并且API再次失败,我会再次将showErrorAlert设为真.但现在,没有显示ngbAlert.

怎样才能使ngbAlert再次显示?

推荐答案

请在下面找到一个正常运行的示例,其中关闭alert 运行良好.我将事件(closed)="showErrorAlert = false"添加到了HTML中,它开始正常工作!

HTML

<ngb-alert
  class="veri-alert"
  [type]="alert.type"
  [dismissible]="true"
  *ngIf="showErrorAlert"
  #staticAlert
  (closed)="showErrorAlert = false"
>
  <span class="icon icon-alert-dark"></span>
  <span class="pl-2">{{ alert.message }}</span>
</ngb-alert>

<button (click)="clickAction()">open alert</button>

TS

import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { NgbAlertModule } from '@ng-booTStrap/ng-booTStrap';

@Component({
  selector: 'ngbd-alert-basic',
  standalone: true,
  imporTS: [NgbAlertModule, CommonModule],
  templateUrl: './alert-basic.HTML',
})
export class NgbdAlertBasic {
  showErrorAlert = false;
  alert = {
    type: 'success',
    message: 'it is working!',
  };

  clickAction() {
    this.showErrorAlert = true;
  }
}

stackblitz

Angular相关问答推荐

如何将CDkDropList与Angular FormArray和FormGroup一起使用?

如何在Angular 17 SSR中处理Swiper 11 Web组件事件

如何在ANGLING v17中使用鞋带样式组件?

筛选器不会从文本输入触发更改事件

截获火灾前调用公共接口

Angular 懒惰地加载一个服务

如何修复不允许的HttpErrorResponse 405?

Angular 为什么延迟加载返回空路径?

Angular Signals 如何影响 RXJS Observables 以及在这种情况下变化检测会发生什么

应该显示在表格中的嵌套循环

Angular:为什么在 ngAfterContentInit 之后变量未定义?

BehaviorSubject 在 Angular 中制作数据之间的时间表(或计时器)

关闭 Dynamsoft Web Twain 弹出窗口

在 angular2 中,如何获取在为 @Input 发送的对象上更改的属性的 onChanges

Angular2:更新数组时*ngFor不更新

Angular 服务decorator 提供在延迟加载的根效果

取消订阅服务中的http observable

Angular 5 手动导入语言环境

如何在 ANGULAR 2 中提交表单时重置表单验证

Angular Material2 主题 - 如何设置应用程序背景?