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.
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 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.
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 Can you modify your hot fix works with the fork of http://plnkr.co/edit/Or0Ik1S1HtvUVq3YYdhC
@lygstate it's a rather trivial, I just copy-pasted the workaround above. http://plnkr.co/edit/GyCLkQ0aFgFpPr9QUSiO
👍
Is it hard to fix it?
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
+1
+1
+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.
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.
@blckshrk this gives me an exception:
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'
@mebibou Can't help you without the code ;-)
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?
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 thanks for this, this.segment._inputUpdated(); will also update the selected tab, was missing this one
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
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.