It is uncertain if the changes can be applied to the body or html elements directly, but they can definitely be made to the root component.
- To achieve this, create a service that is injected into the root component.
- This service should have a state, possibly using a
BehaviorSubject
.
- Access the service and update the state whenever the value of
isModalOpened
changes.
- In the root component, monitor this change and modify the parameter values of the components accordingly.
- Within the root component's HTML, adjust the class values based on the component's parameter values.
Update: Changing background color from within an inner component.
app.component.css
.red{
background: red;
}
.white{
background: white;
}
.green{
background: green;
}
app.component.html
<div [ngClass]="backgroundColor" ></div>
app.component.ts
constructor(private statusService: StatusService) {
this.subscription = this.statusService.getColor()
.subscribe(color => { this.backgroundColor = color; });
}
status.service.ts
private color = new Subject<any>();
public setColor(newColor){
this.color.next(newColor);
}
public getColor(){
return this.color.asObservable();
}
child.component.ts
export class ChildComponent {
constructor(private statusService: StatusService) {}
setColor(color:string){
this.statusService.setColor(color);
}
}
By calling setColor with 'red', 'green', or 'white' as the color variable, the background color of the root component will change accordingly.