I have developed a component test1 which contains a button and a <p>Hello World</p>. When I click on the button the background color of the paragraph changes. But now I want to change the background color in app.component when I click on the button. I tried to solve the problem with @Input, but unfortunately it does not work yet. Can you please help me? My Stackblitz
My code:
// test1.component
// HTML
<button type="button" (click)="testHandler()">Change Color</button>
<p [class.toggled]="classToggled">Hello World</p>
// CSS
.toggled {
background-color: blue !important;
}
// TS
public classToggled = false;
testHandler() {
this.classToggled = true;
}
// app.component
// HTML
<div class="wrapper" [class.test1]="classToggled">
<router-outlet></router-outlet>
</div>
// CSS
.test1 {
background-color: red !important;
}
// TS
@Input() public classToggled = false;
There are multiple ways to do it.
For unrelated components, you could use a singleton service to share a styling object between the components and bind it using
[ngStyle].styling.service.ts
app.component.ts
app.component.html
test.component.ts
test.component.html
Working example: Stackblitz