在角度8中更改复选框检查值时触发更改

更改了值,但未触发更改事件。我想在更改ischecked时触发更改

在HTML

 <input class="inputStyle" type="checkbox" [checked]="isChecked"  id="actA" tabindex="0" (change)="onAct($event)">

还有其他功能(以ts为单位)

onChangeoption(lang) {
            this.isChecked = true;     `enter code here`   
 }
评论
  • uautem
    uautem 回复

    What you are trying to do is trigger a change event after you have programatically changed the value of the input first and this is not going to work.

    我可以建议两种方法:

    Approach 1: From what you have shown in those snippets, it seems easy to call onAct inside the onChangeoption. If you don't really need the change event object, you can call onAct immediately after you change the value of isChecked.

    Approach 2: If you need the change event object, create a reference of the input element and programatically dispatch a change event

    在您的TypeScript代码中:

      @ViewChild('myCheckbox') myCheckbox;
    
      onChangeoption() {
        this.isChecked = true; `enter code here`
        this.myCheckbox.nativeElement.dispatchEvent(new Event('change'));
      }
    

    并更新您的模板:

    <input #myCheckbox class="inputStyle" type="checkbox" [checked]="isChecked"  
     id="actA" tabindex="0" (change)="onAct($event)">