Home » Javascript » Angular : set method parameter in subscribe

Angular : set method parameter in subscribe

Posted by: admin June 30, 2018 Leave a comment

Questions:

I want to use subscribe in a method and pass my variable(cities) as a parameter to method.

export class MyComponent implements OnInit {
  cities:any;
  constructor(private myApiService: MyApiService) {
  myMethod(this.cities);
  }
  myMethod(value:any)
  {
    this.myApiService.run("myparameter").subscribe((p: any) => {
      value = p.Cities;
      console.log(this.cities);// result-> []
      console.log(p.cities);   // result-> (4) [{…}, {…}, {…}, {…}]
  });
}

My variable didn’t change. I know that I can’t return data from method because of it is an async call.I tried using “ref” (like C#) but It didn’t work. I don’t want to use “map”.

Answers: