【问题标题】:How to hide a bootstrap modal if there is no content inside it in Angular4如果Angular 4中没有内容,如何隐藏引导模式
【发布时间】:2018-11-07 14:59:07
【问题描述】:

我正在使用 Angular 4 电子商务应用程序,在此我需要在购物车中没有产品时隐藏引导模式。

当用户选择了一些添加到 mycart 模态屏幕的产品时。用户可以从 mycart 中删除产品。

在这种情况下,我想这样做,当 mycart 中没有产品时,如果用户单击 mycart 图标它不应该打开模式。

目前它打开一个空的模态屏幕。

appComponent.html

<div class="modal fade modalstyle" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header headerHeight text-white " style="background:rgb(0, 0, 0);font-weight:bold">
        <h6 class="modal-title" id="exampleModalLabel">My Cart Items</h6>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div *ngFor="let products of mycart;let i =index;">
          <div class="container col-sm-12">
            <div class="row">
              <div class="col-sm-4 paddingforimage">
                <img [src]="products['ITEM_IMAGE_PATH']">
              </div>
              <div class="text-info col-sm-6">
                <span>
                  <h6>{{products?.TOTAL_QTY}} &times; {{products?.ITEM_PRICE}} &#8377;</h6>
                </span>
                <span>
                  <h6>{{products?.ITEM_DESC}}</h6>
                </span>
                <span>
                  <h6>{{products?.TOTAL_AMT}} &#8377;</h6>
                </span>
              </div>
              <div class="col-sm-1 text-right">
                <button type="button" class="close closebtn" aria-label="Close" (click)="detele_My_Cart(products?.ITEM_CODE)">
                  <span aria-hidden="true" (click)="detele_My_Cart(products?.ITEM_CODE)">&times;</span>
                </button>
              </div>
            </div>
          </div>
          <hr>
        </div>
        <div class=" container row col-sm-12">
          <div class="col-sm-6">
            <strong>SHIPPING</strong>
          </div>
          <div class="col-sm-6 text-right">0 &#8377;</div>
          <hr>
          <div class="col-sm-6">
            <strong>TOTAL</strong>
          </div>
          <div class="col-sm-6 text-right">{{my_Cart_Total_Amount}} &#8377;</div>
        </div>
        <br>
        <div class="container row col-sm-12" id="wrapper">
          <button type="button" class="btn btn-success buttonSize" data-dismiss="modal" routerLink="/cartsummary">
            <strong>CHECK OUT</strong>
          </button>
        </div>
      </div>
    </div>
  </div>
</div>

图标

<i class="fas fa-cart-plus fa-flip-horizontal text-primary" (click)="get_My_Cart($event)" data-toggle="modal" data-target="#exampleModal" style="font-size:25px;cursor: pointer;"></i>

【问题讨论】:

  • 你能在stackblitz.com上分享你的代码吗
  • @ChellappanV 很难做到,因为如果我可以使用静态 API,API 响应会动态更改..
  • 你可以定义一些静态变量来代替api

标签: angular html typescript bootstrap-4


【解决方案1】:

简单,使用*ngIf 指令?当mycart &amp;&amp; mycart.length 为真时显示模态

例如

<div *ngIf="mycart && mycart.length" class="modal fade modalstyle">
  ....
  ....
<div>

编辑:修复边缘情况,以编程方式触发close 按钮

在模板中添加#closeModalBtn

<button #closeModalBtn type="button" class="close" data-dismiss="modal">
    ...
 </button>   

在component.ts中,以编程方式触发close按钮

@ViewChild('closeModalBtn') closeModalBtn:ElementRef;
 
closeModalIfNoCart() {
  if (!this.mycart || !this.mycart.length)
     this.closeModalBtn.nativeElement.click();
}

detele_My_Cart(id) {
   // todo to remove
   //...
   closeModalIfNoCart();
}

【讨论】:

  • 我已经尝试过了,它运行良好,让我们采取这种情况我已经将一个产品添加到购物车,我从 mycart 模式中删除了现在应用程序页面不工作并且鼠标单击,侧滚动条被禁用。我发现模态窗口由默认关闭图标关闭的键如果我通过验证 mycart 数据关闭模态它的行为是这样的......那么如何修复这个@RitwickDey 跨度>
  • 应用此逻辑后,购物车在双击后打开
  • 谢谢它的工作......只剩下一件事,双击购物车图标后mycart模式打开......我想通过单击打开模式。有可能吗?
  • 我不明白...您正在使用引导程序。为什么必须双击?无论如何,不​​要在你的 Angular 项目中使用 bootstrap.js,更好的选择是使用 3rd 方 Angular-bootstrap 组件。
  • 现在模态窗口在双击购物车图标后打开,在应用上述代码后单击打开之前
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-10
相关资源
最近更新 更多