Calling a function from within a class


#1

Hi everyone,

I just try to get custom with Ionic V2 and got a question regarding calling functions within a class.

I got a directive, that checks wether and element is the last one and then fires a method (slideTest) within the page.ts.

This method contains a method call to “setSlideInClass” with a timeout. But i don’t figure out how to address this method. Neither “setSlideInClass()” nor this.setSlideInClass()" do work.

Please find below a somewhat simplified copy of the class in question, where I stripped of the content that is not related to the problem.

Any hint on what i am missing would be great.

export class ListePage {

    constructor(private navCtrl: NavController, navParams: NavParams, public http: Http) {]

    slideTest(){
        setTimeout(function () { 
            setSlideInClass(elements[i]);						 
        }, 1000 * i);
    }

    setSlideInClass(element){
        element.className += " slide-in"; 
    }
}

#2
setTimeout(() => { this.setSlideInClass(elements[i]); }, 1000 * i);

Arrow function doesn’t create a new scope for this.
To call a function from a class, it’s with this.myMethod

Without arrow function it could be done like this

setTimeout(function() { this.setSlideInClass(elements[i]); }.bind(this), 1000 * i);

#3

Thank you :slight_smile: Works like a charm. And confirms my suspicion that I was missing some kind of reference to “this”.