简体   繁体   English

如何在角度2中存储复选框的已选中或未选中事件

[英]How to store checked or unchecked events of a checkbox in angular 2

<form (submit)="onSubmit()">
  <div class="container">
      <div *ngFor="let initial of initialState,let j = index" >
          <div *ngIf="initial>0">


              <button class="button" type="button">Device {{j}}</button>
              &nbsp; 
              <label class="switch">
                  <input type="checkbox"  id="checkbox_category" checked (change)="search(initial, $event)" />
                  <div class="slider round"></div>
              </label>
          <br/> 
          </div>

          <div *ngIf="initial===0">
              <button class="button" type="button">Device {{j}}</button>
              &nbsp; 
              <label class="switch">
                  <input type="checkbox"  id="checkbox_category" (change)="search(initial, $event)" />
                  <div class="slider round"></div>
              </label>
          <br/> 
          </div>
      </div>
  </div>
  <button class="button" type="button">Submit</button>
 </form>

export class DashboardComponent{

  initialState:any = [1,1,1,0,0,0,1];
  intendedDeviceStatus: any = this.initialState;

  search(category:any, event:any) {

        var index = this.initialState.indexOf(category);
        this.intendedDeviceStatus[index]= (~this.intendedDeviceStatus[index]);      

}

In the above code I have a Initailstatus array which gives me which checkboxes are checked and unchecked ....based on that after displaying checkboxes. 在上面的代码中,我有一个Initailstatus阵列,它给了我该checkboxes显示复选框后选中和未选中....基于这一点。 After Displaying it when I change checked checkbox to uncheck it not getting updated in IntendedDevicesArray . 在我更改选中复选框后显示它以取消选中它未在IntendedDevicesArray更新。

Because initialState is array of number not object. 因为initialState是数字而不是对象的数组。 so the method array.indexOf(value) with find the first element that equal the value. 所以方法array.indexOf(value)找到第一个等于该值的元素。 therefore, it return wrong index; 因此,它返回错误的索引;

Example: 例:

var a = [1,1,1,0, 0 ,0,1];
var b = a[2];
var c = a.indexOf(b); c = 0 not 2

To make your code work. 使代码工作。
change from 改变
<input ... (change)="search(initial, $event)" />
to
<input ... (change)="search(j, $event)" />

and change search function: 并更改搜索功能:

search(index:number, event:any) {
    this.intendedDeviceStatus[index]= this.intendedDeviceStatus[index] ? 1 : 0;      

} }

Beside that, I suggest you use initialState as boolean array insteal of number. 除此之外,我建议你使用initialState作为数字的布尔数组。
user *ngIf="initial" for checked and *ngIf="!initial" for unchecked. user * ngIf =“initial”表示已选中,* ngIf =“!initial”表示未选中。 in search function 在搜索功能

this.intendedDeviceStatus[index] = !this.intendedDeviceStatus[index];

if you want to your code for more short and better, you can write directive that support two way binding for input tag. 如果你想让你的代码更简短更好,你可以编写支持输入标记的双向绑定的指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM