ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

ionic2 ion-segment ion-segment-button not works with ngFor properly.

Open lygstate opened this issue 10 years ago • 37 comments

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.

lygstate avatar Jun 15 '16 15:06 lygstate

Hello, thanks for opening an issue with us! Would you be able to provide a plunker that demonstrates this issue? Thanks for using Ionic!

jgw96 avatar Jun 15 '16 22:06 jgw96

@jgw96 Here is the demo, http://plnkr.co/edit/Or0Ik1S1HtvUVq3YYdhC

Once you change the period days from 3 days to 4 days. Click the 4TH DAY would have no effect.

lygstate avatar Jun 16 '16 00:06 lygstate

As a workaround I manually trigger rebinding logic of Segment component after updating my view state:

import {Segment} from 'ionic-angular';

@Component()
export class MyComponent {
    @ViewChild(Segment)
    private segment: Segment;

    private ngOnInit() {
        this.fetchData().then(data => {
            // Data processing...

            // Hotfix for dynamic ion-segment-buttons issue
            setTimeout(() => {
                if (this.segment) {
                    this.segment.ngAfterViewInit();
                }
            });
        });
    }
}

mnasyrov avatar Jun 19 '16 09:06 mnasyrov

@mnasyrov Can you modify your hot fix works with the fork of http://plnkr.co/edit/Or0Ik1S1HtvUVq3YYdhC

lygstate avatar Jun 19 '16 18:06 lygstate

@lygstate it's a rather trivial, I just copy-pasted the workaround above. http://plnkr.co/edit/GyCLkQ0aFgFpPr9QUSiO

mnasyrov avatar Jun 20 '16 00:06 mnasyrov

👍

bjoernbur avatar Dec 06 '16 10:12 bjoernbur

Is it hard to fix it?

anatoly-spb avatar Dec 08 '16 10:12 anatoly-spb

still open? :-( i've just noticed this bug also both:

  • ngModel is not changed
  • ioncChange event is not fired

it works normally if instead i use static HTML instead of an *ngFor loop

badpenguin avatar Jan 09 '17 20:01 badpenguin

+1

pablozandona avatar Mar 10 '17 14:03 pablozandona

+1

decpio avatar Mar 27 '17 06:03 decpio

+1

Ross-Rawlins avatar Apr 04 '17 09:04 Ross-Rawlins

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.

eduardoyutaka avatar Apr 12 '17 13:04 eduardoyutaka

Hi everyone,

I found a more simplest workaround for this bug. You can add a ngIfon the ion-segment like the following :

<ion-segment *ngIf="businessCollection" [(ngModel)]="businessId">
    <ion-segment-button *ngFor="let business of businessCollection" value="{{ business.id }}">{{ business.title }}</ion-segment-button>
</ion-segment>
cacheService.getAllBusiness().subscribe(data => this.businessCollection = data);

As the DOM is rebuilded only when the businessCollection array is filled (in the subscribe), it seem to fix the issue.

alexbonhomme avatar Apr 17 '17 17:04 alexbonhomme

@blckshrk this gives me an exception:

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'

mebibou avatar May 15 '17 03:05 mebibou

@mebibou Can't help you without the code ;-)

alexbonhomme avatar May 17 '17 09:05 alexbonhomme

The main problem with ion-segment and *ngFor disappear about 6 weeks ago with ionic actualization. Latest version also works well. Can you close the issue?

decpio avatar May 17 '17 10:05 decpio

Still have the issue with dynamically rebuilding ion-segment-button items in Ionic v3.2.1.

Example:

<ion-segment [(ngModel)]="groupIndex">
    <ng-template ngFor [ngForOf]="groups" let-group let-index="index">
        <ion-segment-button [value]="index">
            {{group.title}}
        </ion-segment-button>
    </ng-template>
</ion-segment>

As workaround I use a directive which fix all segments in an app:

import {ContentChildren, Directive, QueryList, Self} from '@angular/core';
import {Segment, SegmentButton} from 'ionic-angular';
import {Subscription} from 'rxjs';

// TODO: Hotfix for dynamic ion-segment-buttons issue (https://github.com/driftyco/ionic/issues/6923).
@Directive({
    selector: 'ion-segment'
})
export class IonSegmentHotfix {
    private subscriptions: Subscription;

    @ContentChildren(SegmentButton)
    buttons: QueryList<SegmentButton>;

    constructor(@Self() private segment: Segment) {
    }

    ngAfterContentInit() {
        this.subscriptions = this.buttons.changes.subscribe(() => this.onChildrenChanged());
    }

    ngOnDestroy() {
        if (this.subscriptions) {
            this.subscriptions.unsubscribe();
            this.subscriptions = null;
        }
    }

    private onChildrenChanged() {
        setTimeout(() => {
            this.segment.ngAfterContentInit();
            this.segment._inputUpdated();
        });
    }
}

mnasyrov avatar May 20 '17 09:05 mnasyrov

@mnasyrov thanks for this, this.segment._inputUpdated(); will also update the selected tab, was missing this one

mebibou avatar May 24 '17 00:05 mebibou

Ran into similar problem. In my case, I have to dynamically create segment buttons after fetching some date from backend, so basically my code looks like

ionViewDidLoad() {
  this.service.get().subscribe(
    res => { this.segmentArray = res; this.selectedSegment = segmentArray[0]; }
  );
<ion-segment [(ngModel)]="selectedSegment">
      <ion-segment-button *ngFor="let index of segmentArray" value="{{segmentArray[index]}}">
        segmentArray[index]
      </ion-segment-button>
</ion-segment>

Then I cannot properly select segment button which didn't show correct appearance None of above workaround fix my issue

I'm on

global packages:

    @ionic/cli-utils : 1.4.0
    Cordova CLI      : 7.0.1
    Ionic CLI        : 3.4.0

local packages:

    @ionic/app-scripts              : 1.3.7
    @ionic/cli-plugin-cordova       : 1.4.0
    @ionic/cli-plugin-ionic-angular : 1.3.1
    Cordova Platforms               : ios 4.4.0
    Ionic Framework                 : ionic-angular 3.4.0

System:

    Node       : v6.11.0
    OS         : macOS Sierra
    Xcode      : Xcode 8.3.3 Build version 8E3004b
    ios-deploy : 1.9.1
    ios-sim    : 5.0.13
    npm        : 3.10.10

qiluo avatar Jun 20 '17 07:06 qiluo

My temp solution

<ion-segment *ngIf="generatedArray">
      <ion-segment-button *ngFor="let item of generatedArray" value="{{generatedArray.someValue1]}}" (tap)="contendChanged(generatedArray.someValue1)">
           generatedArray.someValue2
      </ion-segment-button>
</ion-segment>

(tap) work and you can send data to a function.

helmerdx avatar Jun 22 '17 06:06 helmerdx