简体   繁体   English

Angular 4将复选框绑定到* ngFor中的数组

[英]Angular 4 bind checkbox to array in *ngFor

I would like to add an item to an array whenever a check box is checked in *ngFor I am looking for a neat way of doing this without too much code or using a component method. 我想每当在* ngFor中选中一个复选框时,就将一个项目添加到数组中。我正在寻找一种简洁的方法,无需太多代码或使用组件方法。 I know this used to be really easy in angular version 1 我知道以前在Angular版本1中这很容易

<tr *ngFor="let this_user of RoleUsers.Users">
    ...
    <input type="checkbox" class="custom-control-input" [(ngModel)]="UsersToRemove.Users[this_user.id]" /> <!--[(ngModel)]="" --> <!-- ng-false-value="expression"-->
    ..
</tr>

For this code I get the error ERROR TypeError: Cannot read property '13' of undefined so I think I am very close. 对于此代码,我得到错误ERROR TypeError: Cannot read property '13' of undefined所以我认为我非常接近。

At the moment I am using the user ids to track the keys but if I could have a nice array. 目前,我正在使用用户ID来跟踪键,但是如果可以的话,我可以使用一个不错的数组。 If this is not possible without a component method please provide an example. 如果没有组件方法无法做到这一点,请提供示例。

Update I have managed to get this working with fewer code; 更新我设法用更少的代码来工作;

<input type="checkbox" class="custom-control-input" (change)="AddOrRemoveUser(this_user, $event.target.checked)" />

Then the method; 然后是方法;

AddOrRemoveUserToRemove (user, checked) {
    console.log ("Remove or add: ", user, checked);
}

I think this this the quickest way to do it. 我认为这是最快的方法。

The UsersToRemove object looks like this; UsersToRemove对象如下所示;

class UsersToRemove {
    Users: any[];
    InAction: boolean = false;
}

This is attached to the actual component. 这是附加到实际的组件。

This worked for me but with too much code. 这对我有用,但是代码太多。 Just to answer my question; 只是为了回答我的问题;

@Component({
    ...
})
export class RoleUsersComponent implements OnInit {
    ..
    UsersToRemove: UsersToRemove = new UsersToRemove
  constructor(
     ...
  ) {
        ...
  }

  ...

    AddOrRemoveUser (user, checked) {
        if (checked==true){
            this.UsersToRemove.AddUser(user)
        }else {
            this.UsersToRemove.RemoveUser(user)
        }
        console.log ("UsersToRemove: ", this.UsersToRemove.Users)
    }
}

class UsersToRemove {
    Users: any[];
    constructor( ) { this.Users = []; }
    InAction: boolean = false;
    AddUser (user): void {
        if (!this.UserExists(user)){
            this.Users.push(user);
        }
    }
    RemoveUser (user): void {
        for (var _i = 0; _i < this.Users.length; _i++) {
            if (this.Users[_i].id==user.id){
                this.Users.splice( _i, 1 )
            }
        }
    }
    UserExists (user): boolean {
        let exists = false;
        for (var _i = 0; _i < this.Users.length; _i++) {
            if (this.Users[_i].id==user.id){
                exists = true;
            }
        }
        return exists;
    }
}

Template: 模板:

<tr *ngFor="let this_user of RoleUsers.Users">
  ...
  <th scope="row" *ngIf="UsersToRemove.InAction">
    <label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
      <input type="checkbox" class="custom-control-input" (change)="AddOrRemoveUser (this_user, $event.target.checked)" /> <!--[(ngModel)]="" --> <!-- ng-false-value="expression"-->
      <span class="custom-control-indicator"></span>
    </label>
  </th>
  ...
</tr>

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

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