最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use ngModel in ion-checkbox? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to use with ngModel, but ngModel doesn't work there. My code:

<ion-checkbox *ngFor="#item of items" [(ngModel)]="item.checked">
  {{item.name}}
</ion-checkbox>

But I get an error:

EXCEPTION: Expression 'checked in AddInterestItem@5:2' has changed after it was checked. Previous value: 'false'. Current value: 'false' in [checked in AddInterestItem@5:2]

example data:

this.items = [ 
  {name: 'Dancing', checked: false}, 
  {name: 'Jazz', checked: false}, 
  {name: 'Metal', checked: true}, 
  {name: 'Pop', checked: false}, 
  {name: 'Rock\'n\'Roll', checked: false}, 
  {name: 'Folk Metal', checked: true} 
];

I'm trying to use with ngModel, but ngModel doesn't work there. My code:

<ion-checkbox *ngFor="#item of items" [(ngModel)]="item.checked">
  {{item.name}}
</ion-checkbox>

But I get an error:

EXCEPTION: Expression 'checked in AddInterestItem@5:2' has changed after it was checked. Previous value: 'false'. Current value: 'false' in [checked in AddInterestItem@5:2]

example data:

this.items = [ 
  {name: 'Dancing', checked: false}, 
  {name: 'Jazz', checked: false}, 
  {name: 'Metal', checked: true}, 
  {name: 'Pop', checked: false}, 
  {name: 'Rock\'n\'Roll', checked: false}, 
  {name: 'Folk Metal', checked: true} 
];
Share Improve this question edited Dec 14, 2017 at 13:06 Oleksii Pavlenko 1,3331 gold badge12 silver badges25 bronze badges asked Jan 21, 2016 at 7:26 MaksimMaksim 2,4666 gold badges21 silver badges28 bronze badges 2
  • 1 This error message is not related to ngModel but to ="item.checked". You seem to change item.checked` in a way that is not patible with Angulars default change detection. Please provide more details about how this looks in your code. – Günter Zöchbauer Commented Jan 21, 2016 at 7:42
  • 1 Seems you are changing the values in some unexpected way. This is still not enough information. – Günter Zöchbauer Commented Jan 21, 2016 at 7:48
Add a ment  | 

2 Answers 2

Reset to default 6

I am not familiar with <ion-checkbox>. But when i tried to repeat your story with normal <input type="checkbox" />, I couldn't get success by applying ngFor to input type="checkbox" directly. So what I did, I took <ul><li *ngFor="#item of items" > and put my input type="checkbox" within it and it started working as expected.

I feel like MyAnswer could help you further.

doesn't work in mycase

<input  type="checkbox" *ngFor="#item of items" [(ngModel)]="item.checked" />{{item.name}}  
/* I don't know some error occurs. you can check my plunkr by modifying it/*.

Worked properly

    <ul>
        <li *ngFor="#item of items">{{item.name}}
            <input type="checkbox" [(ngModel)]="item.checked" />
        </li>
    </ul>

Just a quick update for Ionic 4:

HTML:

<ion-checkbox [(ngModel)]="iLikeIt.isChecked"></ion-checkbox>

Ts:

iLikeIt={isChecked:false}
发布评论

评论列表(0)

  1. 暂无评论