Hot to send two parameters on click

Hi, I have this:

<button ion-item *ngFor="let item of items" (click)="goToDetails(item.id)">

I need send two parameters…

I use:

<button ion-item *ngFor="let item of items" (click)="goToDetails(item.id, item.title)">

But now working for me.

Any help please?

2 Likes

In your typescript does ‘goToDetails()’ accept two parameters?
Your function should look something like :

gotoDetails(id:string, title:string){
}

4 Likes

and if you want to set the second parameter as optional:

gotoDetails(id:string, title?:string){
}

or if you want to set a default value, if the second parameter is not set

gotoDetails(id:string, title?:string = 'defaultValue){
}
4 Likes

In your example above :

gotoDetails(id:string, title?:string){
}

Does this make the title parameter optional or the type optional? Does it save you having to write two constructors or does it mean we can use any type, i am thinking the former just want to double check

Title parameter will be optional. You can call goToDetails function with one or two parameters.

Ahh okay, thank you for answering. Perhaps used wrong word constructor but sweet saves you having to write the two functions.

keep in mind that this types are not there after buildprocess, and the compiler do not know if you call this function from a template with invalid parameters. But for testing and better code you should type everything :slight_smile:

1 Like

I wrote a function for login with a if and in the html I pass name and password but the if is never verified, in the log the values are undefined…

HTML

<button ion-button id="login-btn" (click) = "verifyAccess(user, pwd)">Login</button>

(user and password are id’s of text input)

LOGIN.TS

verifyAccess(name:string, pwd:string){...}
1 Like

hi there,
how you can read this parameter in another page thank you