简体   繁体   English

离子3-动态添加离子段

[英]Ionic 3 - adding ion-segment dynamically

I am facing problem on adding the ion-segment values dynamically.. below are my work around.. 我在动态添加离子段值时遇到问题..以下是我的解决方法..

TS file: TS文件:

this.rioTypes = ['ABC', 'XYZ', 'PQR'];
  items1: any = [1, 2, 3, 4];
segmentVal: string = "ABC";

HTML: HTML:

<ion-segment [(ngModel)]="segmentVal" color="primary">
  <ion-segment-button *ngFor="let rioType of rioTypes; let i = index;" value="{{rioType}}">
    {{rioType.title}}
  </ion-segment-button>
</ion-segment>

<div [ngSwitch]="segmentVal" *ngFor="let r of rioTypes">

<ion-list *ngSwitchCase="r" ngSelected="selected">

   <ion-item  *ngFor="let item of items1">
    <h2>TAB - {{item}}</h2>
  </ion-item>
</ion-list>

Seems like everything looks fine but when i run in the device.. ony segments are displaying not below contents.. Im using Ionic v3.19.0 Please anybody help me to get it solve. 似乎一切看起来都很好,但是当我在设备中运行时。.on句段显示的内容不低于内容。.im正在使用Ionic v3.19.0,请有人帮我解决。

following error for below code 以下代码的以下错误

<div [ngSwitch]="segmentVal">

<ion-list *ngFor="let r of rioTypes" *ngSwitchCase="r" 
ngSelected="selected">

 <ion-item  *ngFor="let item of items1">
<h2>TAB - {{item}}</h2>
</ion-item>
</ion-list>

`Uncaught Error: Template parse errors: Can't have multiple template bindings on one element. `未捕获的错误:模板解析错误:一个元素上不能有多个模板绑定。 Use only one attribute named 'template' or prefixed with * (" 仅使用一个名为'template'或以*(“

    <ion-list *ngSwitchCase="r" [ERROR ->]*ngFor="let r of rioTypes" ngSelected="selected">

       <ion-item  *ngFor="let item of "): ng:///AppModule/UserdashboardPage.html@29:36
at syntaxError (vendor.js:80395)
at TemplateParser.parse (vendor.js:104243)
at JitCompiler._parseTemplate (vendor.js:113630)
at JitCompiler._compileTemplate (vendor.js:113605)
at vendor.js:113507
at Set.forEach (<anonymous>)
at JitCompiler._compileComponents (vendor.js:113507)
at vendor.js:113377
at Object.then (vendor.js:80384)
at JitCompiler._compileModuleAndComponents (vendor.js:113376)`

The problem is you have ended up creating multiple ngSwitch blocks with your for loop. 问题是您最终用for循环创建了多个ngSwitch块。 You need only one block with all the cases. 在所有情况下,您只需要一块即可。

Use the loop for your list inside the switch. 将循环用于交换机内部的列表。 You can use ng-container or a plain div for the loop since you cannot have 2 directives in one tag. 您可以在循环中使用ng-container或plain div,因为在一个标记中不能包含2个指令。

<div [ngSwitch]="segmentVal">
<ng-container *ngFor="let r of rioTypes" >
<ion-list *ngSwitchCase="r">

   <ion-item  *ngFor="let item of items1">
    <h2>TAB - {{item}}</h2>
  </ion-item>
</ion-list>
</ng-container>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM