我有一个primeng手风琴,手风琴 Headers 有一个复选框控件 . 每当我选中/取消选中复选框时,手风琴选项卡会自动打开/关闭
Is there any way to prevent expand/collapse on click on checkbox ? It should expand/collapse on header other than check/uncheck check box ?
这是因为 Event Bubbling 而发生的 . 为此需要通过调用 MouseEvent 的 stopPropagation() 来停止eventPropagation .
MouseEvent
stopPropagation()
手风琴html代码示例
<p-accordion> <p-accordionTab header="Godfather I" [selected]="true"> <p-header> <span>Godfather I <input type="checkbox" (click)="onClick($event)"> </span> </p-header> Some Text </p-accordionTab> </p-accordion>
对应的 component ts代码 .
onClick($event: MouseEvent){ $event.stopPropagation(); }
对于Reference,添加了stackblitz代码示例 .
这就是我解决这个问题的方法 . 这是因为事件冒泡而发生的 . 所以当你点击子元素时 . 事件传播到其父级,依此类推 . 所以只需在事件中使用停止传播 . 它将阻止手风琴上的点击事件 . 下面的代码供您参考 .
与我使用的复选框代码Accordian(onChange)方法 .
<p-accordionTab> <p-header> <div class="ui-g" style="width:250px;margin-bottom:10px"> <div class="ui-g-12"><p-checkbox name="group1" #ck value="New York" label="New York" [(ngModel)]="selectedCities" (onChange)="checkChange($event)" inputId="ny"></p-checkbox></div> </div> </p-header> </p-accordionTab>
component.ts
selectedCities: string[] = []; //Simply you have to to stop propogation here. checkChange(e:any){ console.log(e); // true or false. event.stopPropagation(); // component will have direct access to event here. }
2 回答
这是因为 Event Bubbling 而发生的 . 为此需要通过调用
MouseEvent
的stopPropagation()
来停止eventPropagation .手风琴html代码示例
对应的 component ts代码 .
对于Reference,添加了stackblitz代码示例 .
这就是我解决这个问题的方法 . 这是因为事件冒泡而发生的 . 所以当你点击子元素时 . 事件传播到其父级,依此类推 . 所以只需在事件中使用停止传播 . 它将阻止手风琴上的点击事件 . 下面的代码供您参考 .
与我使用的复选框代码Accordian(onChange)方法 .
component.ts