title: Angular2 - OnInit : Values returned from Service' subscribe function does not get assigned to Component field/s
body: <p>I'm trying out Angular2 and have been following their tutorials. </p>
  
  <p>I currently have a Service that gets data from a json server:</p>
  
  
  
  <pre class="lang-ts prettyprint-override"><code>import { Injectable } from '@angular/core';
  import { Http, Response } from '@angular/http';
  
  import { Observable } from 'rxjs/Observable';
  
  import { User } from './user';
  
  @Injectable()
  export class UserService {
    constructor(private http: Http) {}
  
    private usersUrl = 'http://localhost:3000/users';
  
    getUsers(): Observable&lt;User[]&gt; {
      return this.http.get(this.usersUrl) //the request won't go out until something subscribes to the observable
                      .map(this.extractData)
                      .catch(this.handleError); // pass an error message back to the component for presentation to the user but only if we can say something the user can understand and act upon
    }
  
    private extractData(response: Response) {
      if (response.status &lt; 200 || response.status &gt;= 300) {
         throw new Error('Bad response status: ' + response.status);
      }
      let body = response.json(); // parse JSON string into JavaScript objects
  
      return body.data || { };
    }
  
    private handleError (error: any) {
      // In a real world app, we might send the error to remote logging infrastructure before returning/sending the error
     let errMsg = error.message || 'Server error'; // transform the error into a user-friendly message
  
     return Observable.throw(errMsg); // returns the message in a new, failed observable
    }
  
  }
  </code></pre>
  
  <p>And my Component:</p>
  
  <pre class="lang-ts prettyprint-override"><code>import { Component, OnInit } from '@angular/core';
  
  import { User } from '../common/user/user';
  import { UserService } from '../common/user/user.service';
  
  @Component({
    selector: 'app-nav',
    templateUrl: '/app/nav/nav.component.html',
    styleUrls: ['/app/nav/nav.component.css']
  })
  export class NavComponent implements OnInit {
    errorMsg: string;
    users: User[];
  
    constructor(private userService: UserService) { }
  
    getUsers() {
      this.userService
      .getUsers()
      .subscribe(
        function(users) {
          console.log('users ' + users);
          this.users = users;
          console.log('this.users ' + this.users);
        }, function(error) {
          console.log('error ' + error);
        });
      // users =&gt; this.users = users,
      // error =&gt; this.errorMsg = &lt;any&gt;error);
    }
  
    ngOnInit() {
      this.getUsers();
      console.log('ngOnit after getUsers() ' + this.users);
    }
  }
  </code></pre>
  
  <p>My problem is, the data returned from the subscribed function is not being passed/assigned to my Component property 'users' after the getUsers() invocation completes. I do know that the data is returned to the component from the service method call because I'm able to log the data within the userService().getUsers method. The weird thing is, the console.log invocation on my ngOnInit is being printed on my dev console first before the console.logs within my getUsers method, although I invoke the getUsers first:</p>
  
  <pre class="lang-ts prettyprint-override"><code>  ngOnInit() {
      this.getUsers();
      console.log('ngOnit after getUsers() ' + this.users);
    }
  </code></pre>
  
  <p>Dev console screenshot: </p>
  
  <p><img src="https://i.stack.imgur.com/CW3ms.png" alt=""></p>