【问题标题】:Bootstrap modal opens after double click rather open at first click in Angular 4双击后引导模式打开而不是在Angular 4中首次单击时打开
【发布时间】:2018-11-10 07:30:37
【问题描述】:

我在一个电子商务应用程序中工作,在这个应用程序中,我有购物车图标。我想要做的是当我点击购物车图标时,我想显示一个带有用户选择的产品数据的模式屏幕。

但在我的情况下,第一次在页面加载后双击打开模态屏幕,然后单击打开。

图标:

<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>

模态:

<div *ngIf="mycart && mycart.length" 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 #closeModalBtn 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>

组件:

get_My_Cart($event) {

 this.publicIp.v4().then(ip => {
        this.CartdataService.get_My_Cart_Products(ip).subscribe(
            data => {
                this.mycart = data['Table']
                this.my_Cart_Total_Amount = data['Table1'][0]['TOTAL_AMOUNT']
            });
    });
}

在模态屏幕中我有一个验证,如果数据绑定在模态屏幕中,则打开模态屏幕,否则不会打开。(如果购物车为空,模态屏幕将不会打开)。

*ngIf="mycart && mycart.length"

如果我从模式屏幕中删除此验证,它可以工作,但它总是打开(如果购物车中没有产品并且用户点击它打开的购物车图标但我想限制它。)

谁能告诉我如何处理这个问题..

【问题讨论】:

    标签: html angular typescript bootstrap-4


    【解决方案1】:

    代码流不正确。

    无论如何,我认为,您正在使用jQuery 和Bootstrap.js。 (坏主意)

    一旦数据来自服务器,您可以通过编程方式触发模式using $('#exampleModal').modal('show')。

    declare var $ :any;
    
    get_My_Cart($event) {
       this.publicIp.v4().then(ip => {
            this.CartdataService.get_My_Cart_Products(ip).subscribe(
                data => {
                    this.mycart = data['Table']
                    this.my_Cart_Total_Amount = data['Table1'][0]['TOTAL_AMOUNT']
                    if(this.mycart && this.mycart.length)
                        $('#exampleModal').modal('show')
                });
        });
    }
    

    但是在这种情况下有一个简单的解决方法。尝试切换 CSS 的 display 属性,而不是使用 *ngIf 从 DOM 中删除它。

    <div [ngStyle]="{display: mycart && mycart.length ? 'block':'none'}" class="modal fade modalstyle">
    
    </div>
    

    【讨论】:

    • 在控制台收到此错误 ERROR 错误:找不到不同的支持对象“无”
    • 寻找您的解决方案@RitwickDey
    • 抱歉迟到了(这是我的考试时间)......无论如何,有一个错字......"display: mycart &amp;&amp; mycart.length ? 'block':'none' "....顺便说一句,试试他们。第一个应该工作......
    • 成功了,能解释一下为什么之前没有成功,现在是怎么做到的吗?
    • 首先:工作正常,但是当我进入浏览器控制台时,会出现类似“ERROR TypeError: $(...).modal is not a function”@RitwickDey 的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    相关资源
    最近更新 更多