ionic-framework: ionic2 ion-segment ion-segment-button not works with ngFor properly.
Short description of the problem:
When using ion-segment, ion-segment-button with ngFor, Once the length of dataArray are increased, the ion-segment-button corresponded increased entries of dataArray can not be clicked.
<ion-segment [(ngModel)]="valve.selectDay">
<ion-segment-button *ngFor="let info of dataArray" value="{{info.day}}">{{info.day+1}}天</ion-segment-button>
</ion-segment>
What behavior are you expecting?
Once push entries into the end of dataArray ,the newly ion-segment-button can be click with selection properly.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 7
- Comments: 37 (4 by maintainers)
Hi everyone,
I found a more simplest workaround for this bug. You can add a
ngIfon theion-segmentlike the following :As the DOM is rebuilded only when the
businessCollectionarray is filled (in the subscribe), it seem to fix the issue.👍
Still have the issue with dynamically rebuilding
ion-segment-buttonitems in Ionic v3.2.1.Example:
As workaround I use a directive which fix all segments in an app:
still open? 😦 i’ve just noticed this bug also both:
it works normally if instead i use static HTML instead of an *ngFor loop
Is it hard to fix it?
As a workaround I manually trigger rebinding logic of Segment component after updating my view state:
+1
+1
Thanks for the issue and sorry for the lack of response! This issue has recently been fixed in Ionic for the v4 release. I just tested it on our
corebranch and I can see it working now:I’ll try to come back and update this issue when we have something for you all to test out.
Related commits: https://github.com/ionic-team/ionic/commit/242960dc29e9efdc180108925a45680cb3191a7a https://github.com/ionic-team/ionic/commit/1e6e481958fea99e5c29628d0339cccde1305302
My temp solution
(tap) work and you can send data to a function.
If you use an *ngFor or *ngIf structural directive to generate your
<ion-segment-button>elements, and you change either:But you can - it all works fine except for applying the
segment-activatedCSS class to the selected button and removing it from all the others. The options, data model, and DOM all update fine, which is why none of my messing w/ngOnChangesor ChangeDetectionRef.detectChanges()had any impact - everything was already up to date except for the CSS!The
segment-activatedclass gets stuck on whichever element existed in the previous list, if any. If your updated segment data source has identical values & number of buttons, no problem. But, if all the segment values change, thesegment-activatedclass won’t appear for any of them.After waaay too long screwing around with this, I finally hacked together this ugly approach to fix the problem, and it’ll do since I’ve wasted so much time on it …
It’s ugly & ol’skool, but it gets the job done and I have no more time to make it fancy …
Would be nice if somebody could take a look at how the CSS classes get applied after changes to the
<ion-segment>component and fix this.Thanks ~
+1
I ran into the same problem and solved it by adding a click event like this:
<ion-segment [(ngModel)]=“valve.selectDay”> <ion-segment-button *ngFor=“let info of dataArray” value=“{{info.day}}” (click)=“setInfo(info.day)”>{{selectedDay+1}}天</ion-segment-button> </ion-segment>
and then you can define the setInfo() function and a selectedDay variable at the component as something like:
selectedDay; if (dataArray[0].info.day != nil) { selectedDay = dataArray[0].info.day; } … setInfo(day) { selectedDay = day+1; }
I am not really sure how your code is since the demo is different than the original problem, but the logic behind is adding a click event in order to change the segmented button content so that it matches the button’s value.
@mccumb01
Thanks for your solution.The only issue i am facing right now is i am unable to make first ionic segment button active.After click it’s working properly.
How can i make first segment button active when page loads first time.
Mycode:
I had to do a more RxJS-friendly version which I based on @mccumb01’s excellent comment. Rather than
event.targetto get the segment, I useViewChild. Below is the general idea. Who’s looking forward to a new Stencil segment?