【问题标题】:ion-select using pipe transform, how to set default value?离子选择使用管道变换,如何设置默认值?
【发布时间】:2022-01-02 07:04:31
【问题描述】:

我在 <ion-select-option *ngFor="let item of collection | myPipe:someValueToFilter" [value]="item"> 上使用管道转换,管道将执行一些过滤、映射和排序。

如何在管道转换后的转换集合中将默认值设置为<ion-select [(ngModel)]="selectedItem"> 内的selectedItem? (假设转换后的集合中的第一项)

【问题讨论】:

  • 你能提供你的代码吗?
  • @MohammadBabaei 我编辑了一些 html 代码,关于管道,它只是做一些普通的过滤、映射和排序

标签: angular ionic-framework pipe ngfor ion-select


【解决方案1】:

我认为您可以在component.ts 文件中使用您的管道,而不是像这样的模板:

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

然后在您的方法或ngOnInit 钩子中使用您的管道过滤您的数据并在您想要的地方使用它的值,例如,设置为选定的项目。

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

【讨论】:

  • 抱歉回复晚了,谢谢。我最终使用了与您的答案类似的 service.ts。毕竟,我发现它仍然是控制数据的最简单方法。交给我懒惰的大脑模块
猜你喜欢
  • 2018-07-20
  • 2017-04-30
  • 2019-04-29
  • 1970-01-01
  • 2021-05-13
  • 2019-09-18
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多