I have a simple Flask app running that returns a string
and my Angular2 code has been cobbled together from various tutorials that I've seen online webservices.services.ts
import { Component, OnInit, Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import {HttpModule} from '@angular/http';
import {JsonpModule} from '@angular/http';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class WebService {
constructor(private http: Http, private router: Router,private _jsonp: JsonpModule) { }
public getDataFromBackend() {//
return this.http.get('http://localhost:5000/getstuff')
.map(data=>{
data.json();
console.log(data.json());
return data.json();
})
}
}
App.Component.ts
import { Component , OnInit} from '@angular/core';
import { WebService } from './webservices/webservices.services';
import { Http, Response } from '@angular/http';
import { Router } from '@angular/router';
import {HttpModule} from '@angular/http';
import { RouterModule } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css'],
providers: [WebService]
})
export class AppComponent implements OnInit {
constructor(private http: Http, private webservice: WebService, private router: Router) { }
title: string = 'My first angular2-google-maps project';
lat: number = 45.478418;
lng: number = -122.209007;
msgBackend: string = "";
public ngOnInit(){
}
public getData(){
this.webservice.getDataFromBackend()
.subscribe(
(data) => this.handleData(data),
() => console.log('Write to console output')
);
}
private handleData(data: Response) {
if (data.status === 200) {
let receivedData = data.json();
console.log(receivedData);
this.msgBackend= receivedData;
return this.msgBackend;
}
}
}
So when my app runs, it does hit the Flask app and returns a 200. Also, the console does indeed write "Write to console output". I also tried adding an observable Observable but that didn't work. I think I'm just not grasping Angular2 http and async stuff.
EDIT: console.log(receivedData); why isn't it going to the console?