Unable to change values in ion-select-multiple

戏子无情 提交于 2021-02-11 14:46:23

问题


I am using ion-select-multiple. It works perfectly fine normally. I have a requirement to pre select few options in it. I searched the web and found the solution, but after pre assigning the values, now when I try to change, it gives me error.

Here is my home.html code:

 <ion-select multiple="true" okText="Okay" cancelText="Dismiss" [(ngModel)]="followers" [value]="{$value: followers}">
    <ion-select-option value="brown">Brown</ion-select-option>
    <ion-select-option value="blonde">Blonde</ion-select-option>
    <ion-select-option value="black">Black</ion-select-option>
  </ion-select>

home.ts

followers = ['brown','black'];

Please note, Brown and Black are pre selected, but if I try to select/de-select anything from list, it gives error -

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'model: 12w 3'. Current value: 'model: [object Object]'.

This error goes away if I remove this code: [value]="{$value: followers}".

How do I manage this error?

Add error screenshot:


回答1:


Add thid piece of code to your compoent.ts file...

import { Component, Input, ChangeDetectionStrategy,ChangeDetectorRef } from '@angular/core';

 @Component({
   selector: 'component',
   templateUrl: 'component.html',
   changeDetection: ChangeDetectionStrategy.OnPush
 })

 constructor(private cdRef:ChangeDetectorRef){} 
 ngAfterViewInit() {
    this.cdRef.detectChanges();
 }


来源:https://stackoverflow.com/questions/60875448/unable-to-change-values-in-ion-select-multiple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!