0

I have a func which calling in ngDoCheck(). That func calls several another func who reassign my variable. But variable change does only in funcs, not a global variable. myVariable allways 0.

myVariable = 0;

ngDoCheck(): any {
    const word: string = this.form.get('word').value;
    this.PasswordStrengthMeter(word, this.myVariable);
    console.log('Value: ' + this.myVariable);
  }


Mainfunc(word: string, myVariable: number): any {
    this.SecondaryFunc(word, myVariable);
    this.AnotherSecondaryFunc(word, myVariable);
    
  }
2
  • Return new value and in caller assign to this.myVariable = this.SecondaryFunc Commented Oct 5, 2020 at 8:51
  • number attributes are passed by value, not by reference. Commented Oct 5, 2020 at 9:11

2 Answers 2

1

If it is in the same component, why do you passing that variable to each function? just use it in that function as it's a global variable.

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

Comments

1

The value of myVariable is being passed to Mainfunc, not a reference.

If you'd like to change the global variable, use this.myVariable directly from your other SecondaryFunc and AnotherSecondaryFunc.

myVariable = 0;

...

Mainfunc(word: string): any {
    this.SecondaryFunc(word);
    this.AnotherSecondaryFunc(word);
}

SecondaryFunc(word: string): any {
    this.myVariable = 5;
}

AnotherSecondaryFunc(word: string): any {
    this.myVariable = 6;
}

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.