我正在尝试有条件地设置CSS样式,就像更改为true,背景为红色,当更改为false时,背景为黑色。但我不知道如何通过检查更改标志让主机绑定知道这一点。演示代码如下:
import { Component, HostBinding } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title: string;
change: boolean;
@HostBinding('class.someChange') isChanged() {
this.change = true;
return this.change;
}
constructor() {
this.title = 'Tour of Heroes';
}
}
并且对于HTML文件是这样的:
<h1>{{title}}</h1>
<nav>
<a routerLink = "/dashboard"> Dashboard </a>
<a routerLink = "/heroes"> Heroes </a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>
CSS文件如下:
:host {
width: 100%;
height: 100%;
display: block;
background-color: rgb(0, 255, 149);
padding: 20px;
color: #ffffff;
text-align: center;
font-weight: bold;
&.someChange {
background-color: red;
}
}