Ivar_the_clueless
Ivar_the_clueless

Reputation: 15

ion-select using pipe transform, how to set default value?

I'm using pipe transform on <ion-select-option *ngFor="let item of collection | myPipe:someValueToFilter" [value]="item"> and the pipe will do some filter, map and sort.

How can I set default value to selectedItem within <ion-select [(ngModel)]="selectedItem"> in transformed collection after pipe transforming? (Let's say the first item in transformed collection)

Upvotes: 0

Views: 316

Answers (1)

Mohammad Babaei
Mohammad Babaei

Reputation: 493

I think you can use your pipe in component.ts file instead of the template like this:

@Component({
  ...
  providers: [MyPipe]
})
...
contractor(private myPipe: MyPipe)

then in your method or ngOnInit hook use your pipe to filter your data and use its value where you want, for example, set to the selected item.

 handleMyData(data): any {
  this.collection = this.myPipe.transform(data, filters)
  this.selectedItem = this.collection[0]
}

Upvotes: 0

Related Questions