Par*_*ain 4 eventemitter bootstrap-modal angular
EventEmitter使用bootstrap模式处理angular2 ,但每当我通过子组件的事件传递一些参数时,只有在Bootstrap模式的情况下,角度才能解决纠正参数,否则一切正常.为什么?我做错了什么?
调用子组件编码(父组件) -
<ul>
<li *ngFor='#value of abc'>
<child-component (everySecond)="everySecond(value)"></child-component>{{view}}
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
子组件编码 -
<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">×</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 ---->
Run Code Online (Sandbox Code Playgroud)
plunkr在这里http://plnkr.co/edit/2rlvpMpcTd0Gk8DelwoP?p=preview
每个ChildComponent元素使用相同的元素id="delete_category".引导模式使用该ID匹配的第一个,这就是永远的第一ChildComponent与demoInput == 1
更改两行可以修复它
<div class="modal fade" id="delete_category{{demoInput}}" role="dialog">
Run Code Online (Sandbox Code Playgroud)
<button type="button" class="btn btn-danger" data-toggle="modal"
attr.data-target="#delete_category{{demoInput}}">
Run Code Online (Sandbox Code Playgroud)
另请注意添加attr.的属性绑定.
更新
你可以使用一个随机值demoInput在id="delete_category{{demoInput}}".
似乎没有必要使用与中相同的值everySecond(value).只有在一个中使用id和attr.data-target需要相同的值,ChildComponent同时它们需要在不同的ChileComponents 之间不同.
我会用
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.
}
Run Code Online (Sandbox Code Playgroud)
<div class="modal fade" id="delete_category{{componentId}}" role="dialog">
Run Code Online (Sandbox Code Playgroud)
<button type="button" class="btn btn-danger" data-toggle="modal"
attr.data-target="#delete_category{{componentId}}">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
317 次 |
| 最近记录: |