Angular 4 with TypeScript: passing parameters by reference
05:29 10 Sep 2017

I'm just starting to develop a web application with angular 4 with TypeScript language. My question is: if I initialize a string variable with the value of a data field in my class, it looks like the value is passed for copy and not for reference.

Is there a way to pass the parameter for reference?

Example:

export class MyObject {
   string1: string;
   string2: string;
}

export class MyClass {
   myString: string;
   object: MyObject;
   array[]: MyObject[];

   constructor() {
      this.array = [{
       string1: this.myString;
       string2: this.myString;
      }];
   }

}

If I tried to change the value of field this.myString and then use string interpolation in my HTML template

{{myString}}

{{array[0].string1}} {{array[0].string2}}

the only value that has changed is myString, while the other two values ​​remained the same as myString's first value. This is because it seems that the passage of the parameters has been done for value and not by reference.

Sorry if the question is trivial, but I'm learning TypeScript right now

angular typescript pass-by-reference pass-by-value