4

I'm trying to implement Ionic2's Slides, but I can't find how to set the settings:

ion-slides "options" has been deprecated. Please use ion-slide's input properties instead.

I can find the 'input properties' (e.g. autoplay, speed, direction), but I don't have a clue where to place this. Every example I find does [options]="slideOptions" where slideOptions are the settings, but that results in no result other that the deprecated notice.

I'm new to ionic v2 and typescript, I could probally get a hacky solution to work, but I want to do it right.


The HTML in ion-content:

<ion-slides [options]="slideOptions">
    <ion-slide *ngFor="let item of items">
        <img width="20%"src="{{item.thumb.source}}">
    </ion-slide>
</ion-slides>

And the simplified class: import { Slides } from 'ionic-angular';

@Component({
  selector: 'page-example',
  templateUrl: 'example.html'
})

export class Example {
    public items: any;
    private slideOptions: any;

    @ViewChild(Slides) slides: Slides;

    constructor(private navCtrl: NavController, public navParams: NavParams) {
        this.items = [];
        this.albumInfo = navParams.get('item');

        this.getSlides();
    }

    // This does nothing:
    goToSlide() {
        this.slides.slideTo(2, 500);
    }
    // This  does nothing:
    ngAfterViewInit() {
        this.slides.autoplay = 2000;
    }


    // Simple example of getting the slides
    makeGetRequest():Promise<string> {      
        /* Get the items code, populates this.items
    }
}
Martijn
  • 15,791
  • 4
  • 36
  • 68

2 Answers2

7

ion-slides have been changed. Make sure you have updated to the latest ionic version (2.0) Input properties listed in the site can be directly used in the html. Eg:

<ion-slides pager loop autoplay="2000">
 <ion-slide> ... </ion-slide>
</ion-slides>

To use properties other than those listed in input properties, you can use variable in your HTML.

<ion-slides #slides> 
   ...
</ion-slides>

and use it in TS:

 import { Component, ViewChild } from '@angular/core';
 import {Slides} from 'ionic-angular'

export class Example {
    @ViewChild(Slides) slides: Slides;
    constructor() {}
    ngAfterViewInit() {
        this.slides.autoplay = 2000;
        this.slides.autoplayDisableOnInteraction = false;
   }
}

In your case, ngAfterViewInit does nothing as you have not defined your variable in your html as <ion-slides #slides>

AishApp
  • 4,142
  • 2
  • 29
  • 33
  • I've implemented your code, and got "Cannot read property 'hasAttribute' of undefined". I can change all settings (at least the ones I've tried), but can't set autoplay. Which is the one I wanted. Thanks for your reply – Martijn Feb 02 '17 at 10:35
  • still not working.. Have you updated your ionic version? – AishApp Feb 02 '17 at 10:44
  • 1
    It's all working, only autoplay isn't. Autoplay returns the error, other settings work fine. And I'm using the current up to date version, 2.1.4 :) – Martijn Feb 02 '17 at 10:57
  • But it works for me without issue. Can you update your question with your latest code? – AishApp Feb 02 '17 at 11:02
  • It's quite similar to the topic really. I've found that it might be because there are no slides on init, so I've moved the `autoplay` to be set after the `makeGetRequest()` promise is done. It doesn't work, but it's also no longer giving an error – Martijn Feb 02 '17 at 11:26
  • Setting the autoplay value now works, but when I trigger `this.slides.startAutoplay();` it fails again, with the same error. – Martijn Feb 02 '17 at 11:37
  • I've now build my own autoplay with a setInterval, works perfectly :) – Martijn Feb 02 '17 at 11:52
0

Input properties mean just the properties set in html of ion-slides and Output events are the html events. For example:

<ion-slides [autoplay]="num">
    <ion-slide *ngFor="let item of items">
        <img width="20%"src="{{item.thumb.source}}">
    </ion-slide>
</ion-slides>

where num is variable in the component with a particular number value.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • I've tried this, which results in "Cannot read property 'hasAttribute' of undefined". I've used the code I have in my topic, and tried it without all the Slides codes (so just the html variant. Both result in this error – Martijn Feb 02 '17 at 09:16
  • I've found more suggestions as you've posted, but those don't work properly anymore, somewhere along the road there has been a quite undocumented change :) – Martijn Feb 02 '17 at 10:36
  • 1
    https://github.com/driftyco/ionic/blob/master/CHANGELOG.md#breaking-changes ionic has breaking changes in slides from RC5.. maybe thats where all these bugs started.. – Suraj Rao Feb 02 '17 at 10:39