There is a service called DisplaysService that contains a variable called moneys
. Whenever I make a purchase using the buy button
on the buy component, I update the value of this variable in the service from the component. However, the updated value does not reflect in the sell component even though I retrieve the value from the service. The sell component displays the original value of the DisplaysService variable.
displays.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class DisplaysService {
moneys = 50000;
constructor() { }
}
buy.component.ts
import { DisplaysService } from './../displays.service';
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-buy',
templateUrl: './buy.component.html',
styleUrls: ['./buy.component.css']
})
export class BuyComponent implements OnInit {
moneys : number;
Response = {};
selectedcurrency : string = "Select Currency"
constructor(private http : HttpClient, private displaysservice : DisplaysService) { }
ngOnInit(): void {
this.http.get('https://api.coinranking.com/v1/public/coins?base=EUR&timePeriod=7d')
.subscribe(Response => {
console.log(Response)
this.Response = Response
});
}
ChangeCurrencySymbol (newcurrency : string) {
this.selectedcurrency = newcurrency;
}
ChangeMoneys (moneysspent : Number) {
this.moneys = this.displaysservice.moneys - Number(moneyssp$ent);
this.displaysservice.moneys = this.moneys;
}
}
sell.component.ts
import { DisplaysService } from './../displays.service';
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-sell',
templateUrl: './sell.component.html',
styleUrls: ['./sell.component.css']
})
export class SellComponent implements OnInit {
moneys : number;
Response = {};
selectedcurrency : string = "Select Currency"
constructor(private http : HttpClient, private displaysservice : DisplaysService) { }
ngOnInit(): void {
this.http.get('https://api.coinranking.com/v1/public/coins?base=EUR&timePeriod=7d')
.subscribe(Response => {
console.log(Response)
this.Response = Response
});
}
ChangeCurrencySymbol (newcurrency : string) {
this.selectedcurrency = newcurrency;
}
ChangeMoneys (moneysspent : Number) {
this.moneys = this.displaysservice.moneys + Number(moneysspent);
this.displaysservice.moneys = this.moneys;
}
}