How to detect when Ionic Slides is ready?











up vote
0
down vote

favorite












I'm using the Ionic ion-slides component to display a SKU carousel. The current SKU must be selected among a SKU list.



The thing is the Ionic Slides does not appear to be ready immediately, even called inside the Angular ngAfterViewInit hook. I have to use a timeout first.



My config info :



   ionic (Ionic CLI)  : 4.0.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/lib/node_modules/ionic)
Ionic Framework : ionic-angular 3.1.1
@ionic/app-scripts : 1.3.7

System:

NodeJS : v6.10.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/bin/node)
npm : 3.10.10
OS : macOS High Sierra


My component file :



import { Component, OnInit, ViewChild, Input, Output, EventEmitter, AfterViewInit } from '@angular/core';
import { Logger, AppConfig } from '@instoreapps/core/dist';
import { Slides } from 'ionic-angular';

import { SkuResult } from '../../models/item.model';

import * as _ from 'lodash';


/**
* Angular component to show size variation (or capacity in ml) of one product in carousel.
* @implements OnInit
*/
@Component({
selector: 'isa-item-size',
templateUrl: './item-size.component.html',
providers: [
{ provide: Logger, useFactory: (config: AppConfig) => new Logger(config, 'item size'), deps: [AppConfig] }
]
})
export class ItemSize implements OnInit, AfterViewInit {

@ViewChild(Slides) public containersSlides: Slides;

@Input() public defaultSkuIndex: number;
@Input() private skuList: SkuResult;
@Output() private containerSelected: EventEmitter<any> = new EventEmitter();

/**
* Index of the selected container.
* Serve to apply specific css style
* on selected container.
*/
private selectedIndex: number = 0;

constructor(private logger: Logger) { }

/**
* Select default sku available and emit
* event to parent component to select it inside the carousel
*/
ngAfterViewInit(): void {

this.logger.trace('Entering ngAfterViewInit method', this.selectedIndex, this.defaultSkuIndex);

setTimeout(() => {
this.selectContainer(this.defaultSkuIndex);
}, 300);
}

/**
* Event triggered after the slide change is done
*/
public onSlideDidChange(event: any): void {
this.containerSelected.emit(this.defaultSkuIndex);
this.selectedIndex = this.defaultSkuIndex;
}

/**
* Function to use from parent component @see {ItemDetails}
* to set selected container and make carousel slide to it.
* @param {number} index of sku.
*/
selectContainer(index: number): void {

this.logger.trace('Entering selectContainer method with index : ', index, this.selectedIndex);

if (index !== this.selectedIndex) {

this.selectedIndex = index;
this.containersSlides.slideTo(_.floor(index / 2));
}
}
}


My HTML file :



<ion-list id="items-colors">

<ion-list-header id="items-colors-header">
{{ 'items.availableColors' | translate }}
</ion-list-header>

<ion-item>
<ion-slides id="item-colors" (ionSlideDidChange)="onSlideDidChange($event)">
<ion-slide *ngFor="let item of skuList; let i = index">
...
</ion-slide>
</ion-slides>
</ion-item>
</ion-list>


I'd like to suppress the timeout and call the selectContainer method directly, but when I do I have the following error :



undefined is not an object (evaluating 's._snapGrid.length')"


Does anyone know a workaround ?










share|improve this question
























  • are you getting any error? why do you think its not ready?
    – Suraj Rao
    Nov 8 at 11:17










  • Sorry, I forgot that part. I edited my first post.
    – rguerin
    Nov 8 at 12:21










  • is this a page or a component? Is the lifecycle method called?
    – Suraj Rao
    Nov 8 at 13:59










  • Yes, since it has the @Component tag. And yes I can see the log inside the ngAfterViewInit method.
    – rguerin
    Nov 8 at 14:02










  • and skuList? do you have the data?
    – Suraj Rao
    Nov 8 at 15:15















up vote
0
down vote

favorite












I'm using the Ionic ion-slides component to display a SKU carousel. The current SKU must be selected among a SKU list.



The thing is the Ionic Slides does not appear to be ready immediately, even called inside the Angular ngAfterViewInit hook. I have to use a timeout first.



My config info :



   ionic (Ionic CLI)  : 4.0.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/lib/node_modules/ionic)
Ionic Framework : ionic-angular 3.1.1
@ionic/app-scripts : 1.3.7

System:

NodeJS : v6.10.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/bin/node)
npm : 3.10.10
OS : macOS High Sierra


My component file :



import { Component, OnInit, ViewChild, Input, Output, EventEmitter, AfterViewInit } from '@angular/core';
import { Logger, AppConfig } from '@instoreapps/core/dist';
import { Slides } from 'ionic-angular';

import { SkuResult } from '../../models/item.model';

import * as _ from 'lodash';


/**
* Angular component to show size variation (or capacity in ml) of one product in carousel.
* @implements OnInit
*/
@Component({
selector: 'isa-item-size',
templateUrl: './item-size.component.html',
providers: [
{ provide: Logger, useFactory: (config: AppConfig) => new Logger(config, 'item size'), deps: [AppConfig] }
]
})
export class ItemSize implements OnInit, AfterViewInit {

@ViewChild(Slides) public containersSlides: Slides;

@Input() public defaultSkuIndex: number;
@Input() private skuList: SkuResult;
@Output() private containerSelected: EventEmitter<any> = new EventEmitter();

/**
* Index of the selected container.
* Serve to apply specific css style
* on selected container.
*/
private selectedIndex: number = 0;

constructor(private logger: Logger) { }

/**
* Select default sku available and emit
* event to parent component to select it inside the carousel
*/
ngAfterViewInit(): void {

this.logger.trace('Entering ngAfterViewInit method', this.selectedIndex, this.defaultSkuIndex);

setTimeout(() => {
this.selectContainer(this.defaultSkuIndex);
}, 300);
}

/**
* Event triggered after the slide change is done
*/
public onSlideDidChange(event: any): void {
this.containerSelected.emit(this.defaultSkuIndex);
this.selectedIndex = this.defaultSkuIndex;
}

/**
* Function to use from parent component @see {ItemDetails}
* to set selected container and make carousel slide to it.
* @param {number} index of sku.
*/
selectContainer(index: number): void {

this.logger.trace('Entering selectContainer method with index : ', index, this.selectedIndex);

if (index !== this.selectedIndex) {

this.selectedIndex = index;
this.containersSlides.slideTo(_.floor(index / 2));
}
}
}


My HTML file :



<ion-list id="items-colors">

<ion-list-header id="items-colors-header">
{{ 'items.availableColors' | translate }}
</ion-list-header>

<ion-item>
<ion-slides id="item-colors" (ionSlideDidChange)="onSlideDidChange($event)">
<ion-slide *ngFor="let item of skuList; let i = index">
...
</ion-slide>
</ion-slides>
</ion-item>
</ion-list>


I'd like to suppress the timeout and call the selectContainer method directly, but when I do I have the following error :



undefined is not an object (evaluating 's._snapGrid.length')"


Does anyone know a workaround ?










share|improve this question
























  • are you getting any error? why do you think its not ready?
    – Suraj Rao
    Nov 8 at 11:17










  • Sorry, I forgot that part. I edited my first post.
    – rguerin
    Nov 8 at 12:21










  • is this a page or a component? Is the lifecycle method called?
    – Suraj Rao
    Nov 8 at 13:59










  • Yes, since it has the @Component tag. And yes I can see the log inside the ngAfterViewInit method.
    – rguerin
    Nov 8 at 14:02










  • and skuList? do you have the data?
    – Suraj Rao
    Nov 8 at 15:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm using the Ionic ion-slides component to display a SKU carousel. The current SKU must be selected among a SKU list.



The thing is the Ionic Slides does not appear to be ready immediately, even called inside the Angular ngAfterViewInit hook. I have to use a timeout first.



My config info :



   ionic (Ionic CLI)  : 4.0.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/lib/node_modules/ionic)
Ionic Framework : ionic-angular 3.1.1
@ionic/app-scripts : 1.3.7

System:

NodeJS : v6.10.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/bin/node)
npm : 3.10.10
OS : macOS High Sierra


My component file :



import { Component, OnInit, ViewChild, Input, Output, EventEmitter, AfterViewInit } from '@angular/core';
import { Logger, AppConfig } from '@instoreapps/core/dist';
import { Slides } from 'ionic-angular';

import { SkuResult } from '../../models/item.model';

import * as _ from 'lodash';


/**
* Angular component to show size variation (or capacity in ml) of one product in carousel.
* @implements OnInit
*/
@Component({
selector: 'isa-item-size',
templateUrl: './item-size.component.html',
providers: [
{ provide: Logger, useFactory: (config: AppConfig) => new Logger(config, 'item size'), deps: [AppConfig] }
]
})
export class ItemSize implements OnInit, AfterViewInit {

@ViewChild(Slides) public containersSlides: Slides;

@Input() public defaultSkuIndex: number;
@Input() private skuList: SkuResult;
@Output() private containerSelected: EventEmitter<any> = new EventEmitter();

/**
* Index of the selected container.
* Serve to apply specific css style
* on selected container.
*/
private selectedIndex: number = 0;

constructor(private logger: Logger) { }

/**
* Select default sku available and emit
* event to parent component to select it inside the carousel
*/
ngAfterViewInit(): void {

this.logger.trace('Entering ngAfterViewInit method', this.selectedIndex, this.defaultSkuIndex);

setTimeout(() => {
this.selectContainer(this.defaultSkuIndex);
}, 300);
}

/**
* Event triggered after the slide change is done
*/
public onSlideDidChange(event: any): void {
this.containerSelected.emit(this.defaultSkuIndex);
this.selectedIndex = this.defaultSkuIndex;
}

/**
* Function to use from parent component @see {ItemDetails}
* to set selected container and make carousel slide to it.
* @param {number} index of sku.
*/
selectContainer(index: number): void {

this.logger.trace('Entering selectContainer method with index : ', index, this.selectedIndex);

if (index !== this.selectedIndex) {

this.selectedIndex = index;
this.containersSlides.slideTo(_.floor(index / 2));
}
}
}


My HTML file :



<ion-list id="items-colors">

<ion-list-header id="items-colors-header">
{{ 'items.availableColors' | translate }}
</ion-list-header>

<ion-item>
<ion-slides id="item-colors" (ionSlideDidChange)="onSlideDidChange($event)">
<ion-slide *ngFor="let item of skuList; let i = index">
...
</ion-slide>
</ion-slides>
</ion-item>
</ion-list>


I'd like to suppress the timeout and call the selectContainer method directly, but when I do I have the following error :



undefined is not an object (evaluating 's._snapGrid.length')"


Does anyone know a workaround ?










share|improve this question















I'm using the Ionic ion-slides component to display a SKU carousel. The current SKU must be selected among a SKU list.



The thing is the Ionic Slides does not appear to be ready immediately, even called inside the Angular ngAfterViewInit hook. I have to use a timeout first.



My config info :



   ionic (Ionic CLI)  : 4.0.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/lib/node_modules/ionic)
Ionic Framework : ionic-angular 3.1.1
@ionic/app-scripts : 1.3.7

System:

NodeJS : v6.10.1 (/Users/rguerin/.nvm/versions/node/v6.10.1/bin/node)
npm : 3.10.10
OS : macOS High Sierra


My component file :



import { Component, OnInit, ViewChild, Input, Output, EventEmitter, AfterViewInit } from '@angular/core';
import { Logger, AppConfig } from '@instoreapps/core/dist';
import { Slides } from 'ionic-angular';

import { SkuResult } from '../../models/item.model';

import * as _ from 'lodash';


/**
* Angular component to show size variation (or capacity in ml) of one product in carousel.
* @implements OnInit
*/
@Component({
selector: 'isa-item-size',
templateUrl: './item-size.component.html',
providers: [
{ provide: Logger, useFactory: (config: AppConfig) => new Logger(config, 'item size'), deps: [AppConfig] }
]
})
export class ItemSize implements OnInit, AfterViewInit {

@ViewChild(Slides) public containersSlides: Slides;

@Input() public defaultSkuIndex: number;
@Input() private skuList: SkuResult;
@Output() private containerSelected: EventEmitter<any> = new EventEmitter();

/**
* Index of the selected container.
* Serve to apply specific css style
* on selected container.
*/
private selectedIndex: number = 0;

constructor(private logger: Logger) { }

/**
* Select default sku available and emit
* event to parent component to select it inside the carousel
*/
ngAfterViewInit(): void {

this.logger.trace('Entering ngAfterViewInit method', this.selectedIndex, this.defaultSkuIndex);

setTimeout(() => {
this.selectContainer(this.defaultSkuIndex);
}, 300);
}

/**
* Event triggered after the slide change is done
*/
public onSlideDidChange(event: any): void {
this.containerSelected.emit(this.defaultSkuIndex);
this.selectedIndex = this.defaultSkuIndex;
}

/**
* Function to use from parent component @see {ItemDetails}
* to set selected container and make carousel slide to it.
* @param {number} index of sku.
*/
selectContainer(index: number): void {

this.logger.trace('Entering selectContainer method with index : ', index, this.selectedIndex);

if (index !== this.selectedIndex) {

this.selectedIndex = index;
this.containersSlides.slideTo(_.floor(index / 2));
}
}
}


My HTML file :



<ion-list id="items-colors">

<ion-list-header id="items-colors-header">
{{ 'items.availableColors' | translate }}
</ion-list-header>

<ion-item>
<ion-slides id="item-colors" (ionSlideDidChange)="onSlideDidChange($event)">
<ion-slide *ngFor="let item of skuList; let i = index">
...
</ion-slide>
</ion-slides>
</ion-item>
</ion-list>


I'd like to suppress the timeout and call the selectContainer method directly, but when I do I have the following error :



undefined is not an object (evaluating 's._snapGrid.length')"


Does anyone know a workaround ?







javascript angular typescript ionic-framework ionic3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 12:20

























asked Nov 8 at 11:05









rguerin

15210




15210












  • are you getting any error? why do you think its not ready?
    – Suraj Rao
    Nov 8 at 11:17










  • Sorry, I forgot that part. I edited my first post.
    – rguerin
    Nov 8 at 12:21










  • is this a page or a component? Is the lifecycle method called?
    – Suraj Rao
    Nov 8 at 13:59










  • Yes, since it has the @Component tag. And yes I can see the log inside the ngAfterViewInit method.
    – rguerin
    Nov 8 at 14:02










  • and skuList? do you have the data?
    – Suraj Rao
    Nov 8 at 15:15


















  • are you getting any error? why do you think its not ready?
    – Suraj Rao
    Nov 8 at 11:17










  • Sorry, I forgot that part. I edited my first post.
    – rguerin
    Nov 8 at 12:21










  • is this a page or a component? Is the lifecycle method called?
    – Suraj Rao
    Nov 8 at 13:59










  • Yes, since it has the @Component tag. And yes I can see the log inside the ngAfterViewInit method.
    – rguerin
    Nov 8 at 14:02










  • and skuList? do you have the data?
    – Suraj Rao
    Nov 8 at 15:15
















are you getting any error? why do you think its not ready?
– Suraj Rao
Nov 8 at 11:17




are you getting any error? why do you think its not ready?
– Suraj Rao
Nov 8 at 11:17












Sorry, I forgot that part. I edited my first post.
– rguerin
Nov 8 at 12:21




Sorry, I forgot that part. I edited my first post.
– rguerin
Nov 8 at 12:21












is this a page or a component? Is the lifecycle method called?
– Suraj Rao
Nov 8 at 13:59




is this a page or a component? Is the lifecycle method called?
– Suraj Rao
Nov 8 at 13:59












Yes, since it has the @Component tag. And yes I can see the log inside the ngAfterViewInit method.
– rguerin
Nov 8 at 14:02




Yes, since it has the @Component tag. And yes I can see the log inside the ngAfterViewInit method.
– rguerin
Nov 8 at 14:02












and skuList? do you have the data?
– Suraj Rao
Nov 8 at 15:15




and skuList? do you have the data?
– Suraj Rao
Nov 8 at 15:15

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206480%2fhow-to-detect-when-ionic-slides-is-ready%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206480%2fhow-to-detect-when-ionic-slides-is-ready%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff