[英]How to reassign variable in angular9?
I have a func which calling in ngDoCheck()
.我有一个调用
ngDoCheck()
的函数。 That func calls several another func who reassign my variable.该 func 调用了另一个重新分配我的变量的 func。 But variable change does only in funcs, not a global variable.
但是变量更改只在 funcs 中进行,而不是全局变量。
myVariable
allways 0. myVariable
始终为 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);
}
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.
只需在该函数中使用它,因为它是一个全局变量。
The value of myVariable
is being passed to Mainfunc
, not a reference . myVariable
的值被传递给Mainfunc
,而不是引用。
If you'd like to change the global variable, use this.myVariable
directly from your other SecondaryFunc
and AnotherSecondaryFunc
.如果您想更改全局变量,
this.myVariable
直接从其他SecondaryFunc
和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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.