自定义复选框触发两次

问题描述 投票:0回答:1
angular dom-events
1个回答
0
投票

我有一种强烈的感觉,它的发生是因为输入元素上的

[ngModel]
。您使用它来设置
value
:) 的值,然后在
toggleCheck()
函数中再次设置它(实际上是恢复它)。

<div 
  class="ultim-checkbox" 
  [class.readonly]="readonly || disabled" 
  [class.checked]="checked" 
  [class.intermediate]="intermediate" 
  (click)="toggleCheck($event)">
  <!-- Either remove ngModel >
  <input
    type="checkbox"
    tabindex="0"
    [required]="required"
    [ngModel]="REMOVE_ME"
  />
  <span>
    <!-- ... --> 
  </span>
</div>

或者不要这样做:

public toggleCheck(event: MouseEvent): any {
  if (this.checkboxGroup) {
    this.checkboxGroup.addOrRemove(this.value);
  } else {
    // Or don't set the value here 
    // Although I'm not sure how the condition above affects this.
    this.value = !this.value;
  }
  this.intermediate = false;
}
© www.soinside.com 2019 - 2024. All rights reserved.