EventEmitter 未使用 bootstrap 模式解析正确的参数

EventEmitter does't resolve correct parameter with bootstrap modal

使用 bootstrap 模态在 angular2 EventEmitter 上工作,但是每当我通过子组件的事件发射器传递一些参数时 angular 仅在Bootstrap 模式的情况,否则一切正常。为什么 ?我做错了什么?

调用子组件编码(父组件)-

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

子组件编码 -

<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 同样在这里 http://plnkr.co/edit/2rlvpMpcTd0Gk8DelwoP?p=preview

每个 ChildComponent 元素使用相同的 id="delete_category"。 Bootstrap 模态使用第一个与 id 匹配的,并且始终是第一个 ChildComponentdemoInput == 1

改两行就解决了

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

另请注意为属性绑定添加的 attr.

更新

您可以在 id="delete_category{{demoInput}}" 中为 demoInput 使用随机值。

似乎没有必要使用与everySecond(value)中相同的值。只有 idattr.data-target 中使用的值需要在一个 ChildComponent 中相同,同时它们需要在不同的 ChileComponent 中不同。

我会用

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