Let's say I have a variable called isVisible. And I have a method called
ReverseVariable(variable: boolean)
{
variable = !variable;
}
I want to call this method from a template like
<button (click)="ReverseVariable(isVisible)"></button>
I want to give it isVisible in the parameters and have isVisible reverse itself. Something like the following example is not an option
ReverseVisibility()
{
this.isVisible = !this.isVisible;
}
Is there any way that I can pass the variable by reference?
Not with a primitive data type like a boolean. What you could do is make a non-primitive like an object
isVisible = {
flag: true
}
Then toggle that in your function
ReverseVisibility(isVisible)
{
isVisible.flag = !isVisible.flag;
}
Here is plnkr demonstrating this ( https://plnkr.co/edit/VYEimNoHZvGxeE4S2W4L?p=preview )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.