Vishal Kamal
Vishal Kamal

Reputation: 1124

How to pass a callback as a function

I'm trying to pass a callback as a function parameter.

For example:

public abc1(doc:any){        
        console.log('abc1');    
    }

    public abc2(model:any){
        console.log('abcd2');    
    }

 xyz.load(id.replace('/', ''), abc1, abc2);

Here abc1 and abc2 my callback functions and I'm trying to pass these functions in xyz.load as 2 and 3 arg. All are in the same component.

Upvotes: 1

Views: 6404

Answers (1)

Maximilian Riegler
Maximilian Riegler

Reputation: 23506

You could define the load method like so:

load(id: string, callback1: (doc: any) => void, callback2: (model: any) => void) {
    // do your stuff here
    callback1(theDocument);
    callback2(theModel);
}

And call it like this:

xyz.load(id.replace('/', ''), abc1, abc2);

Upvotes: 3

Related Questions