问题
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