One thing that we can do is to add styles directly to HTML elements that live within our component. However, in this lesson we see that style classes added to your template HTML within your component get applied to an inner <div> and not your component host. We will learn how to use :host(...) and @HostBinding to add styling directly to the component host itself.

import { Component, HostBinding } from '@angular/core';

@Component({
selector: 'host-styling',
template: `
<div>
I'm a div that wants to be styled
<button (click)="redFont = !redFont">toggle</button>
</div>
`,
styles: [
`
/* Apply this style if .yellow-style is added to the component tag itself */
:host {
background-color: yellow;
display:block;
} :host(.red-font) {
color: red;
}
`
]
})
export class HostStylingComponent {
@HostBinding ('class.red-font') redFont = true; }

We can use :host(<class-name>) with @HostBinding, it will add .red-font class to the host element based on condition.

最新文章

  1. 判断是否是有效的IPV4地址
  2. PHPCMS-V9 获取一级栏目、二级栏目、三级栏目
  3. [转]C++ DLL远程注入与卸载函数
  4. qooxdoo 3.0 发布,JavaScript 的 GUI 框架
  5. C++中的namespace用法
  6. struts2于validate要使用
  7. 爱你不容易 —— Stream详解
  8. C++ struct 初始化的问题
  9. iOS-UI控件优化
  10. webpack4.0各个击破(1)—— html部分
  11. JavaScript里面的arguments到底是个啥?
  12. linux 学习笔记 groupadd创建组
  13. Quick Union
  14. JavaWeb核心之Servlet
  15. BZOJ.3004.[SDOI2012]吊灯(结论)
  16. 菜鸟学JS(三)——自动隐藏的悬浮框
  17. 查看虚拟机CENTOS7 的 IP 地址和命令
  18. hostname命令详解
  19. [Training Video - 4] [Groovy] String Functions
  20. 【视频编解码&#183;学习笔记】4. H.264的码流封装格式 &amp; 提取NAL有效数据

热门文章

  1. 【使用uWSGI和Nginx来设置Django和你的Web服务器】
  2. 紫书 例题 9-1 UVa 1025 ( DAG的动态规划)
  3. Swift编程语言初探
  4. 測试password强度
  5. 手动连接数据库(jdbc)
  6. 使用PHP来压缩CSS文件
  7. C#接口,类,集成
  8. Zabbix自动发现与自动注册.
  9. 爬虫爬数据时,post数据乱码解决的方法
  10. widget-移除底部小部件内容