【发布时间】:2020-09-20 08:19:21
【问题描述】:
我开发了一个组件 test1,其中包含一个按钮和一个 <p>Hello World</p>。当我单击按钮时,段落的背景颜色会发生变化。但是现在我想在单击按钮时更改 app.component 中的背景颜色。我试图用@Input 解决这个问题,但不幸的是它还不起作用。你能帮我么? My Stackblitz
我的代码:
// 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;
【问题讨论】:
-
你的组件 test1 是在 app 组件里面吗?
-
是的,没错。
-
然后你可以使用 Output 来代替 Input 装饰器并发出值。
标签: angular typescript angular-event-emitter