Michael Rountree
Michael Rountree

Reputation: 325

Passing Multipe Rest Parameters from HTML function Call to TypeScript Function In Angular2

I have an Angular2 project in which I am trying to construct an object with an unknown number of parameters from an HTML button click which calls a function. This function is using rest parameters in order to accept an unknown number of parameters as shown below:

addArguments(...args: any[]) {
    for (var i = 0, arg; arg = args[i]; i++) {
        console.log("add values args:", ...arg);
    }
}

This function is called from an HTML button class as below:

<button type="submit" class="btn" (click)="addArguments(6, 'Argument1', 'Argument2')">
     Add
</button>

The problem is, when I run this code, the console output is add values args: 6.

I cannot figure out why it is not passing through the additional 'Argument1' and 'Argument2'.

Upvotes: 0

Views: 973

Answers (1)

seidme
seidme

Reputation: 13048

You're not constructing the for loop correctly.

Should be:

addArguments(...args: any[]){
    for (var i = 0, i < args.length; i++) {
        console.log("arg:", args[i]);
    }

    console.log("args:", args); // [6, "Argument1", "Argument2"]
}

Upvotes: 1

Related Questions