呼喚遠(yuǎn)方
2023-04-20 17:15:13
我有一個(gè)調(diào)用ngDoCheck(). 該 func 調(diào)用了幾個(gè)重新分配我的變量的 func。但是變量變化只在函數(shù)中發(fā)生,而不是全局變量。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); }
2 回答

精慕HU
TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
如果它在同一個(gè)組件中,為什么要將該變量傳遞給每個(gè)函數(shù)?只需在該函數(shù)中使用它,因?yàn)樗且粋€(gè)全局變量。

郎朗坤
TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
的值被myVariable傳遞給Mainfunc,而不是引用。
如果您想更改全局變量,請(qǐng)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;
}
添加回答
舉報(bào)
0/150
提交
取消