简体   繁体   English

复选框ngFor Angular2上的双向绑定

[英]two-way binding on checkbox ngFor Angular2

I am creating an Angular2 project and having a problem with two-way binding for a checkbox. 我正在创建一个Angular2项目,并且复选框的双向绑定存在问题。

I have a class called listItem and List like that: 我有一个像listItem和List这样的类:

export class ListItem {
  public count: number;
  public value: string;
  public checked: boolean;

  constructor(count: number, value: string, checked: boolean) {
    this.count = count;
    this.value = value;
    this.checked = checked;
  }
}

export class MyList{
  public category: string;
  public listItem : ListItem [];

  constructor(category: string, listItem : ListItem []) {
    this.category = category;
    this.listItem = listItem ;
  }
}

and I am calling the list from Azure search which is working correctly. 并且我正在正常工作的Azure搜索中调用列表。 the problem is when I just set the value to a checkbox. 问题是当我只是将值设置为复选框时。

<div *ngFor="let list of myList; let listIndex = index;">
  <div *ngFor="let item of list.listItems; let itemIndex = index;">
    <input type="checkbox" [name]="list.category + item.value"
         (change)="item.checked = !item.checked"
         [ngModel]="item.checked" />
  </div
</div>

but the value is always false also onClick. 但onClick的值始终为false。 I tried to use [(ngModel)] but did not work also. 我尝试使用[(ngModel)],但也无法正常工作。 I also tried to make a function: 我也尝试做一个功能:

(change)="oncheck(listIndex, itemIndex)"

oncheck(listIndex: number, itemIndex: number) {
   this.myList[listIndex].listItems[itemIndex].checked =  
           !this.myList[listIndex].listItems[itemIndex].checked;
  } 

but I received this error: 但我收到此错误:

Cannot assign to read-only property 'checked' of object '[object Object]' 无法分配给对象“ [object Object]”的只读属性“ checked”

why is that and how to fix it? 为什么会这样以及如何解决呢? thank you 谢谢

You could use the material2 md-checkbox directive to create styled elements. 您可以使用material2 md-checkbox指令创建样式化的元素。 In my opinion that is not really two-way binding, its just a combination of 1 way binding in both directions (template - data source and data source - template) 我认为这并不是真正的双向绑定,它只是双向双向绑定的组合(模板-数据源和数据源-模板)

UPDATE: I created a small plnkr to reproduce your situation: http://plnkr.co/edit/cr7TokiqSaBGli7mgCBM 更新:我创建了一个小的plnkr来重现您的情况: http ://plnkr.co/edit/cr7TokiqSaBGli7mgCBM

@Component({
  selector: 'my-app',
  template: `
    <div *ngFor="let element of elements; let i = index;">
    <span>Val: {{element}}</span>
      <input type="checkbox"
       [checked]="element" (change)="handleChange(element, i)">
    </div>
  `,
})
export class App {
  elements= [
    false,
    true,
    false,
  ]:

  handleChange(val: boolean, index: number){
    console.log("Index: "+index);
    console.log("Val:  "+val);
    this.elements[index] = !val;
  }

The elements in the list are correctly rendered, but the change events will in some cases modify the values of incorrect positions in the elements array. 列表中的元素正确呈现,但是更改事件在某些情况下会修改elements数组中不正确位置的值。 Ill take a furhter look later 以后再看一眼

UPDATE: refactored plnkr 更新:重构的plnkr

Please check: http://plnkr.co/edit/Ich0g5kzSiQINZjh3VYo 请检查: http : //plnkr.co/edit/Ich0g5kzSiQINZjh3VYo

I made some changes to the plnkr that u sent me. 我对您发送给我的plnkr进行了一些更改。

I changed the iteration variables from const to let (considering that their values arent constant during execution of the loops). 我将迭代变量从const更改为let(考虑到在循环执行期间它们的值不为常数)。

As I mentioned before, most likely there are 2 posibilities: the classes in .ts are being transpiled in a wrong way to .js (class members are being setted as readonly by default), or there is something wrong in the way that you are manually mapping the values to class instances. 正如我之前提到的,很可能存在两种可能性:.ts中的类以错误的方式被转换为.js(默认情况下,类成员被设置为只读),或者您的方式有问题手动将值映射到类实例。

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

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