0

I am new to angular, I tried below i got the response and got in console. but i try to check variable this.users it's coming as undefined in console. I want to assign the service response to this.users. but i am not getting. below is my code.

constructor(private dataService: DataService,private fb: FormBuilder) {
    dataService.getList().subscribe(res => console.log(res)); //works
    this.users=dataService.getList().subscribe(res => this.test=res); 
    console.log(this.users); //undefined
}

how to assign service response to variable, that variable i am using in ngOnInit, please tell me how to do

Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
Ramu
  • 145
  • 2
  • 9
  • Don't assign the result of `subscribe()` to the variable... that's not your users list. – Jeff Mercado Feb 07 '18 at 19:41
  • but i have used the this.users in ngOnInit. how to use service response available in constructor inside the ngOnInit. Please tell me how to do – Ramu Feb 07 '18 at 19:43
  • @Ramu did you even check the duplicate? It has all the information you need :) – AT82 Feb 07 '18 at 19:57
  • @Ramu, when you make this.users=dataService.getList().subscribe... you are asign a variable "this.users" to the Observable. -only you make this if you want unsubscribe-, if you want to asig a variable to the RESULT of an observable, make INTO the subscribe. – Eliseo Feb 08 '18 at 08:24

0 Answers0