【问题标题】:How to pass Dynamic values to the modal using angular 2如何使用角度 2 将动态值传递给模态
【发布时间】:2018-10-20 21:59:38
【问题描述】:

您好,我有一个动态数据,下面以按钮的形式显示它们是代码

              <ng-container *ngFor="let data of mapData">

                <div *ngIf="data.OrderState==='1'" class="ds">
                  <button [id]="data.DrId" class="btn btn-outline secondary">
                    {{data.Name}}
                  </button>
                </div>
              </ng-container>

现在单击动态生成的按钮后,我想将 data.Drid * data.Name 发送到模态输入字段,我该如何调用动态按钮单击模态将这些作为 2 个参数传递并在模态提交按钮上获取该数据

下面是 bootstrap 4 模态代码,我如何在发送参数中应用此代码

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" 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">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

    标签: angular typescript bootstrap-4 bootstrap-modal


    【解决方案1】:

    在模板中你可以让(click) 事件调用一个方法。即

          <ng-container *ngFor="let data of mapData">
    
            <div *ngIf="data.OrderState==='1'" class="ds">
              <button [id]="data.DrId" class="btn btn-outline secondary"
    (click)="buttonClicked(data.DrId, data.Name)">
                {{data.Name}}
              </button>
            </div>
          </ng-container>
    

    在组件中,创建变量drIddrName 并添加buttonClicked 方法,然后为这些变量分配来自buttonClicked 参数的值。即

    buttonClicked(id, name) {
      this.drId = id;
      this.drName = name;
      `$('#exampleModal').modal('show')`
      // You have id and name here, You can make use of these to display anywhere.
    }
    

    要从 JavaScript 处理模态,请参阅此链接 -> https://getbootstrap.com/docs/4.0/components/modal/#via-javascript

    您可以在引导模式模板中绑定这些变量

    <!-- Modal -->
    <div class="modal fade" 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">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <input class="" id ="" [value]="drId"/>
            <input class="" id ="" [value]="drName">
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    

    【讨论】:

    • 但是我如何将它发送到按钮点击的引导模态我如何打开模态
    • @TechieBoy,为此您需要分享您如何使用引导模式。您要发送给 modal 的内容是什么以及如何...
    • 更新问题,我只是放置一个简单的模态,我想在按钮上发送 id、name 到模态,单击模态必须打开并且 id 和名称必须显示在标签中
    • @TechieBoy,这两个模板是否在同一个组件 HTML 中?还是引导模式模板在其他地方?
    • 仅同一个模板
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2021-10-19
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多