0
export class Test implements OnInit {
    getRoleTypeValuesArray:any = [];
    array = [];
    constructor(private ls: myService) {}
    ngOnInit() {
      this.ls.testApi().subscribe(y => {
            this.getRoleTypeValues = y;
            this.getRoleTypeValues.map(y=>{
                var obj = { value: y.id, label: y.roleName }
                this.getRoleTypeValuesArray.push(obj);
            });
            this.array = this.getRoleTypeValuesArray;
            console.log("test1:",this.array);
        });
        console.log("test2:",this.array);
    }
}

I am getting values in test1 console, but not outside test2 console. What is wrong in this code?

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
  • 3
    There's nothing wrong wtih your code; that is how asynchronous code works: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call –  Sep 24 '19 at 13:15
  • @Amy thanks, it is working. I can take `this.array` value further! – Pathik Vejani Sep 25 '19 at 03:51

0 Answers0