I need to create a list of ion-select
based on an array inside an object called myObject.array
.
mypage.page.ts
and mypage.page.html
code listed below. All my ion-select-options
get linked together -
so when I change one ion-select
to Y
all ion-select changes to
Y
etc.ionChange
is fired up only once -
ale inside it I can also see correct data (that only one item inside
a myObject.array is changed).mypage.page.ts
Picture below shows UI after I change only one ion-select
and console log (you can see that multiple ion-select
changed and the console log data doesn't correspond with the UI):
ion-selects
dynamically from an array and don't link them with eachother?Edit:
Indicate whether to send a cookie in a cross-site request by specifying its SameSite attribute
).My code:
<ion-header>
<ion-toolbar>
<ion-title>mypage</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-item *ngFor="let output of myObject.array;let i=index;">
<ion-label> Array object {{i}}</ion-label>
<ion-select
[(ngModel)]="myObject.array[i]"
(ionChange)="change($event, i)"
okText="Set" cancelText="Throw away">
<ion-select-option value="Y"> Yes? </ion-select-option>
<ion-select-option value="N"> No? </ion-select-option>
</ion-select>
</ion-item>
</ion-content>
And this mypage.page.ts
code:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-mypage',
templateUrl: './mypage.page.html',
styleUrls: ['./mypage.page.scss'],
})
export class MypagePage implements OnInit {
myObject = {array: ['Y','Y','Y']};
constructor() {
}
ngOnInit() {
}
change(event, i){
console.log(event);
console.log(i);
console.log(this.myObject);
}
trackBy
is something made to improve change detection performance.
Just for the record here's how one make it "trackBy" index:
Template change
<ion-item *ngFor="let output of myObject.array; let i=index; trackBy:indexTracker;">
Controller change
indexTracker(index: number, value: any) {
return index;
}