1

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?

3
  • Is isVisible a property of the component? Do you say that this.isVisible = !this.isVisible is not an option because the variable name needs to be dynamic or because the variable doesn't belong to the this context? Commented Oct 24, 2017 at 13:29
  • It's not an option because I have way too many isVisible variables and with my last example, I will need a method for every one of them which is not great. It only clutters my component. Commented Oct 24, 2017 at 13:35
  • Ok. Are the variables properties of the component? Commented Oct 24, 2017 at 13:36

1 Answer 1

5

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.