代码之家  ›  专栏  ›  技术社区  ›  Pardeep Jain

EventEmitter无法使用引导模式解析正确的参数

  •  4
  • Pardeep Jain  · 技术社区  · 8 年前

    处理角度2 EventEmitter 使用bootstrap模式,但每当我通过子组件的事件发射器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 ---->
    

    这里也一样 http://plnkr.co/edit/2rlvpMpcTd0Gk8DelwoP?p=preview

    1 回复  |  直到 8 年前
        1
  •  5
  •   Günter Zöchbauer    8 年前

    每个 ChildComponent 元素使用相同的 id="delete_category" .Bootstrap模式使用与id匹配的第一个,并且总是第一个 子组件 具有 demoInput == 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) 。仅在 id attr.data-target 需要在一个内保持相同 子组件 同时,它们需要在不同的 ChileComponent s

    我会用

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