Angular 带输入变量的主机绑定
我试图有条件地设置CSS样式,比如当更改为true时,背景为红色,当更改为false时,背景为黑色。但我不知道如何通过检查更改标志让hostbinding知道这一点。演示代码如下所示:Angular 带输入变量的主机绑定,angular,typescript,sass,Angular,Typescript,Sass,我试图有条件地设置CSS样式,比如当更改为true时,背景为红色,当更改为false时,背景为黑色。但我不知道如何通过检查更改标志让hostbinding知道这一点。演示代码如下所示: import { Component, HostBinding } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.co
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';
}
}
<h1>{{title}}</h1>
<nav>
<a routerLink = "/dashboard"> Dashboard </a>
<a routerLink = "/heroes"> Heroes </a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>
对于HTML文件,如下所示:
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';
}
}
<h1>{{title}}</h1>
<nav>
<a routerLink = "/dashboard"> Dashboard </a>
<a routerLink = "/heroes"> Heroes </a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>