title: angular 2 undefined obtain outside subcribe
body: <p>Good evening to all.</p>
  
  <p>I have an issue when retrieving data from my service to which I have subscribed through subcribe; I get data inside the subscribe function but outside it is UNDEFINED;</p>
  
  <p>Here is the code.</p>
  
  <p><strong>userservice.ts</strong></p>
  
  <pre><code>import { Injectable } from "@angular/core";
  import { Http, Response, Headers } from "@angular/http";
  import 'rxjs/Rx';
  import { Observable } from "rxjs";
  
  @Injectable()
  export class UserService{
    constructor(private http: Http){
  
    }
  
  getRegistrations(): Observable&lt;any&gt; {
      return this.http.get('http://127.0.0.1:8000/api/candidatesL')
        .map(
          (response: Response) =&gt; {
            return response.json().candidates;
          }
        );
    }
  
    }
  </code></pre>
  
  <p><strong>all-registration.ts</strong></p>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  import { NgForm } from "@angular/forms";
  import { Candidate } from "../candidate.interface";
  import { Response } from "@angular/http";
  import { UserService } from "../user.service";
  
  @Component({
    selector: 'app-all-registration',
    templateUrl: './all-registration.component.html',
    styleUrls: ['./all-registration.component.css']
  })
  export class AllRegistrationComponent implements OnInit {
  
    candidates: Candidate[];
  
    constructor(private userService: UserService) {}
  
    ngOnInit() {
  
                  this.getRegistration()
                  console.log(this.candidates);
               }
  
  
      getRegistration(){
      this.userService.getRegistrations()
        .subscribe(
                    (candidates: Candidate[]) =&gt; this.candidates = candidates,
                    (error: Response) =&gt;  console.log(error),
                  )
    }
  
      }
  </code></pre>
  
  <p>When I am inside the .subscribe( ... ) I can display the data but outside I get UNDEFINED.</p>
  
  <p>Please I waiting for your answer... </p>