简体   繁体   English

将复选框的选中值绑定到角度对象的布尔属性

[英]Binding the checked value of a checkbox to boolean attribute of an object in angular

I am trying to bind the value of a checkbox to a boolean variable in an object. 我试图将复选框的值绑定到对象中的布尔变量。 The checkbox is used inside of a table. 该复选框用于表内部。 The functionality that I am trying to achieve is that when I click the checkbox, it should toggle the boolean variable in the object and the result of which would be shown on the html. 我要实现的功能是,当我单击复选框时,它应该切换对象中的布尔变量,其结果将显示在html上。

I am using the below code to achieve the same: 我正在使用下面的代码来实现相同的目的:

HTML: HTML:

<tr *ngFor="let item of getListOfItems(); let i = index">
  <td>{{ i+1 }}</td>
  <td>{{ item.action }}</td>
  <td><input name="chk" type="checkbox" [(ngModel)]="item.isComplete" /></td>
  <td [ngSwitch]="item.isComplete">
    <span *ngSwitchCase="true">Yes</span>
    <span *ngSwitchDefault>No</span>
  </td>
</tr>

Component: 零件:

getListOfItems() {
  var items = [
    new TodoItem('Buy Flowers', false),
    new TodoItem('Get Shoes', false),
    new TodoItem('Collect Tickets', true),
    new TodoItem('Call Joe', false),
  ];

  return items; 
}

TodoItem Class TodoItem类

export class TodoItem {
    action: string;
    isComplete: boolean;

    constructor(action: string, isComplete) {
        this.action = action;
        this.isComplete = isComplete;
    }
}

However, when I run the app, the browser keeps on loading the page and freezes after a period of time. 但是,当我运行该应用程序时,浏览器会继续加载页面,并在一段时间后冻结。 When I comment this checkbox , everything comes back to normal and the code works fine. 当我评论此复选框时,一切恢复正常,代码工作正常。 Could not figure out a solution. 无法解决。 Any help would be appreciated. 任何帮助,将不胜感激。

It has been a while so i may be rusty,but the below should work. 已经有一段时间了,所以我可能会生锈,但是下面的方法应该可以工作。

  <td><input  type="checkbox"  [checked]="item.isComplete" (change)="item.isComplete= !item.isComplete"  /></td>

Make sure you have explicitly decalred isComplete in your TodoItem as boolean 确保已在TodoItem明确将isComplete TodoItemboolean

EDIT: It was just one way binding, It does not suit your use case. 编辑:这只是绑定的一种方法,它不适合您的用例。 Coming to your original code. 来到您的原始代码。

The actual problem is, 实际的问题是

getListOfItems() {
  var items = [
    new TodoItem('Buy Flowers', false),
    new TodoItem('Get Shoes', false),
    new TodoItem('Collect Tickets', true),
    new TodoItem('Call Joe', false),
  ];

  return items; 
}

On each call of <tr *ngFor="let item of getListOfItems(); let i = index"> the method is being called and it will return a new array containing the same items. 在每次调用<tr *ngFor="let item of getListOfItems(); let i = index"><tr *ngFor="let item of getListOfItems(); let i = index">将被调用,该方法将返回一个包含相同项目的新数组。 The result is a never ending for loop. 结果是一个永无止境的for循环。 The best way to avoid this is initialize the array once outside this method and return just the instance in this method like below 避免这种情况的最佳方法是在此方法外初始化数组,然后仅在该方法中返回实例,如下所示

items = [
    new TodoItem('Buy Flowers', false),
    new TodoItem('Get Shoes', false),
    new TodoItem('Collect Tickets', true),
    new TodoItem('Call Joe', false),
  ];

  getListOfItems() {


  return this.items; 
}

I am sure this is just for testing, you will eventually get the data from server in a service class. 我确信这只是为了测试,您最终将在服务类中从服务器获取数据。 For now, this will solve the problem And you original binding will work 现在,这将解决问题,并且您的原始绑定将起作用

<td><input name="chk" type="checkbox" [(ngModel)]="item.isComplete" /></td>

Note: if you are using form group, better to have a different name for each checkbox 注意:如果您使用的是表单组,则最好为每个复选框使用不同的名称

The problem is caused by getListOfItems() returning a new array instance for every call. 该问题是由getListOfItems()为每个调用返回一个新的数组实例引起的。

Rather assign the value of the method to a property: 而是将方法的值分配给属性:

  items: Array<TodoItem>

  ngOnInit(): void {
    this.getListOfItems();
  }

  getListOfItems() {
    var items = [
      new TodoItem('Buy Flowers', false),
      new TodoItem('Get Shoes', false),
      new TodoItem('Collect Tickets', true),
      new TodoItem('Call Joe', false),
    ];

    this.items = items; 
  }

StackBlitz StackBlitz

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

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