繁体   English   中英

如何在 Angular 中使用 @Hostbinding 来设置动态组件的样式?

[英]How do I use @Hostbinding in Angular to style a dynamic component?

我目前正在开发一个 Angular 项目,该项目创建了一堆动态组件,我希望每个组件都具有不同的颜色,具体取决于它在初始化时对服务的调用结果。 该组件的代码如下:

@Component({
  selector: 'app-step',
  templateUrl: './step.component.html',
  styleUrls: ['./step.component.css'],
  providers: [DisplayService, MessageService]
})
export class StepComponent implements OnInit {

//a bunch of other functions and a constructor

findSolutions(defect: Defect): void {
    //this is where I make the call to the service
    this.defectService.getSolutions(defect, this.displayService).subscribe(solutions => {
      if(solutions[0] === undefined) {
          //HERE I want to change the back-ground color of .boxed (element 
          //in the style file) to yellow
        }
        else if(solutions !== undefined) {
          //HERE I want to change the back-ground color of .boxed to green
        }
    });
  }

ngOnInit(): void {
    this.findSolutions(this.defect);
  }
}

有人可以帮我弄清楚如何使用 HostBinding 吗?

为什么不使用 NgClass 添加或删除依赖于组件属性 bool 或值的类?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM