Within the constructor of a component, I am declaring a private variable for an imported component. For example:
constructor(private x: Xcomponent){}
Afterwards, I am calling a function, scanx()
, that is declared inside Xcomponent.
x.scanx()
Within the scanx()
function, there is a variable named ShouldChange
whose initial value is null
. Prior to calling scanx()
, its value is changed by a different function inside Xcomponent to {'some':'something'}
. However, when scanx()
is called from another component, the value of ShouldChange
is null. I have tried to access the values of variables declared and changed in classes (not components) and I retrieve the current values, not the initial ones as it's happening when I try to access the values of imported components.
I am using Visual Studio Code IDE. The language being used is TypeScript.
import { Xcomponent } from '../Xcomponent/xcomponent.component';
import { Globals } from '../globals';
export class parentComponent implements OnInit {
constructor(private x: Xcomponent, private global: Globals) {
}
runThisFunction() {
x.scanx(global.theVariableRetainsEditedValue); // The variable in the global instance retains the changed values.
}
}
// Inside the scanx function of Xcomponent
export class Xcomponent implements OnInit {
ShouldChange = null;
someotherFunction() {
this.ShouldChange = {'some': 'somethingelse'}
}
ngOnInit() {
this.someotherFunction();
this.confirmTheChange();
}
confirmTheChange() {
if (this.ShouldChange === null) {
alert("This never happens");
}
else {
alert("THE VALUE WAS INDEED CHANGED");
}
}
scanx(SomeInput) {
if (this.ShouldChange === null) {
alert("This Should not happen");
} else {
alert("This is not happening. Why?");
}
}
}
I expected the value of the variable ShouldChange
to not be null since it's changed in ngOninit
. But it reflects its initial value when called from an imported instance of the component. However, checking the variable's value from the unimported instance of the component shows that the value has indeed changed as shown in the confirmTheChange()
function.