Shyam Nair
Shyam Nair

Reputation: 159

How to pass Dynamic values to the modal using angular 2

Hi here im having a dynamic data where displaying them in the form of buttons below is the code

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

now after the clicking the button which was dynamically generated i want to send the data.Drid * data.Name to the modal input fields and how can i call the modal on dynamic button click pass these as 2 parameters and get that data on modal submit button

below is bootstrap 4 modal code how i can apply in send param to this

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

Upvotes: 0

Views: 4736

Answers (1)

Basavaraj Bhusani
Basavaraj Bhusani

Reputation: 5673

In template you can have (click) event call a method. i.e.

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

In component, create variables drId and drName and add buttonClicked method, then assign these variables with values from buttonClicked arguments. i.e.

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

To handle modal from JavaScript, see this link -> https://getbootstrap.com/docs/4.0/components/modal/#via-javascript

You can bind these variable in bootstrap modal template

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

Upvotes: 1

Related Questions