Pardeep Jain
Pardeep Jain

Reputation: 86740

EventEmitter does't resolve correct parameter with bootstrap modal

Working on angular2 EventEmitter with bootstrap modal, but whenever i pass some parameters via child component's event emitter angular does not resolve to correct parameters only in case of Bootstrap modal otherwise everything is working fine. why ? m is doing something wrong ?

calling Child component coding (parent component) -

<ul>
  <li *ngFor='#value of abc'> 
    <child-component (everySecond)="everySecond(value)"></child-component>{{view}}
  </li>
</ul> 

child component coding -

<div class="modal fade" id="delete_category" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header modal_header_color">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Delete</h4>
            </div>
            <div class="modal-body">
                <div class="row margin_both">
                    <div class="col-md-12">
                        <div class="row form-group text-center">
                            <span>Are you sure you want to Delete !</span>
                        </div>
                        <div class="row form-group">
                            <div class="text-center">
                                <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
                                <button type="button" class="btn btn-danger" (click)='call()' data-dismiss="modal">Delete</button>  //not working
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#delete_category">
    <span class="glyphicon glyphicon-trash"></span>
</button>  

  <---working ---->
    <button (click)='call()' class='btn btn-info btn-sm'>Working Button</button>  //works fine

  <---working ---->

plunkr for the same here http://plnkr.co/edit/2rlvpMpcTd0Gk8DelwoP?p=preview

Upvotes: 4

Views: 402

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657376

Each ChildComponent element uses the same id="delete_category". Bootstrap modal uses the first that matches the id and that's always the first ChildComponent with demoInput == 1

Changing two lines fixes it

<div class="modal fade" id="delete_category{{demoInput}}" role="dialog">
<button type="button" class="btn btn-danger" data-toggle="modal" 
    attr.data-target="#delete_category{{demoInput}}">

Also notice the added attr. for attribute binding.

update

You could use a random value for demoInput in id="delete_category{{demoInput}}".

There seems to be no need to use the same value as in everySecond(value). Only the value used in id and attr.data-target need to be the same within one ChildComponent while at the same time they need to be different between different ChileComponents.

I would use

class ChildComponent {
  private static cmpId = 0; 

  // getter to make it available for binding from the template 
  // because this doesn't work with statics
  private get componentId() => ChildComponent.prototype.cmpId++;

  // I'm not sure if this ^^^ is the correct way to access static
  // fields in TypeScript.
}
<div class="modal fade" id="delete_category{{componentId}}" role="dialog">
<button type="button" class="btn btn-danger" data-toggle="modal" 
    attr.data-target="#delete_category{{componentId}}">

Upvotes: 5

Related Questions