I am making a small application with Angular7 on Front and Flask on Back, what i am tryig to do is to subscribe for a service then to save the returned data into an object type variable from inside my main AppComponent, then to access this varialbe in all my other components.
Api service
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor(private http: HttpClient) { }
CheckApi(){
return this.http.get('http://localhost:5000/')
}
}
AppComponent
import { Component, OnInit, Injectable } from '@angular/core';
import { ApiService } from './api.service'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'Inbox App';
public text: object
constructor(private data: ApiService){}
ngOnInit(){
this.data.CheckApi().subscribe(data => {
this.text = data
console.log(this.text)
})
}
}
And here is where i am trying to access that object from within another component:
LoginComponent
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
login.component.html
<section>
<legend class="text-center">{{ text }}</legend>
</section>
this.apiService.data;returnsObservableobject not data from server?