Variable returning me undefined which is assigned inside subscribe [duplicate]

1

In the code below, the variable date is returning me undefined , now if I put console.log (this.data); inside of subscribe returns me with the correct value.

import { Component, OnInit } from '@angular/core';
import {Http} from '@angular/http';

@Component({
  selector: 'app-aniversario',
  templateUrl: './aniversario.component.html',
  styleUrls: ['./aniversario.component.css']
})
export class AniversarioComponent implements OnInit {

  data: any;
  constructor(private http: Http) {

  }

  ngOnInit() {
   this.http.get('assets/aniversarios.json').subscribe(res => {
     this.data = res.json();
   });
   console.log(this.data);
  }
}

How can I get the value of res from subscribe ?

    
asked by anonymous 07.07.2017 / 14:47

0 answers