我有一个调用ngDoCheck(). 该 func 调用了几个重新分配我的变量的 func。但是变量变化只在函数中发生,而不是全局变量。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 回答
                            郎朗坤
                            
                                
                            
                        
                        
                                                
                    TA贡献1921条经验 获得超9个赞
的值被myVariable传递给Mainfunc,而不是引用。
如果您想更改全局变量,请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;
}
添加回答
举报
0/150
	提交
		取消
	