I'm trying to share a string value from sibling components(light-switch to navbar). My problem is that the property themeColor
is not getting updated when I emit the new value from my DataService.
Here is my structure for my App.Component.html:
<navbar></navbar>
<banner><light-switch></light-switch></banner>
I'm trying to use a DataService:
import {Injectable} from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class DataService {
private themeColor = new BehaviorSubject<string>("#f5f0f0");
currentThemeColor = this.themeColor.asObservable();
constructor() { }
changeThemeColor(color: string) {
this.themeColor.next(color)
}
}
This is my light-switch.component.ts:
import { Component, OnInit } from '@angular/core';
import {DataService} from "./../Services/DataService";
@Component({
selector: 'light-switch',
templateUrl: './light-switch.component.html',
styleUrls: ['./light-switch.component.scss']
})
export class LightSwitchComponent implements OnInit {
public currentTheme;
public themeColor;
constructor(private sanitization: DomSanitizer, private dataService: DataService) {
this.currentTheme = "dark";
this.themeColor = "#f5f0f0";
}
ngOnInit() {
this.dataService.currentThemeColor.subscribe(color =>{ this.themeColor = color});
}
changeToLight(){
this.dataService.changeThemeColor("black");
}
changeToDark(){
this.dataService.changeThemeColor("#f5f0f0");
}
}
And my navbar.ts:
import { Component, OnInit } from '@angular/core';
import {DataService} from "./../Services/DataService";
@Component({
selector: 'navbar',
templateUrl: './navigation-bar.component.html',
styleUrls: ['./navigation-bar.component.scss']
})
export class NavigationBar implements OnInit {
private themeColor;
constructor(private dataService: DataService) {
}
ngOnInit() {
this.dataService.currentThemeColor.subscribe(color => {this.themeColor = color});
}
}
NavigationBar.html:
<div class="navbar">
<i class="fa fa-github bannerIcon" id="githubIcon" [style.color]='themeColor'></i>
<i class="fa fa-linkedin bannerIcon" id="linkedInIcon" [style.color]='themeColor'></i>
</div>
Light-switch.html:
<div id="lightSwitch">
<button class="btn btn-secondary switchBtn-on" (click)="changeToLight()">On</button>
<button class="btn btn-secondary switchBtn-off">Off</button>
</div>
I have DataService as a provider in my App.Module.ts. When ngOnInit
runs in navbar.ts, it does get the default value I set and when I call the changeThemeColor()
in light-switch.ts, it does go into DataService.ts and change the currentColor
property, but again, the themeColor
property in navbar.ts does not update to that currentColor
property. I suspect there is some sort of event listener I need to correctly get the value from DataService to navbar, but I thought that's what I subscribed for.
Your code looks basically correct.
Is it possible your DataService is not correctly registered and you aren't getting a singleton? Is the providers array for your DataService in a module? Or a component? Or in multiple components?
Ensure the service is only registered (added to the providers array) in one place.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments