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

时间:2016-04-08 07:21:40

标签: angular bootstrap-modal eventemitter

使用bootstrap模式处理angular2 EventEmitter,但每当我通过子组件传递一些参数时,事件发射器角度只有在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 ---->

这里的同一个http://plnkr.co/edit/2rlvpMpcTd0Gk8DelwoP?p=preview

的plunkr

1 个答案:

答案 0 :(得分:5)

每个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.属性绑定。

<强>更新

您可以在demoInput中使用id="delete_category{{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}}">