zoukankan      html  css  js  c++  java
  • [Angular] Use :host-context and the ::ng-deep selector to apply context-based styling

    If you want to style host component. You can use ':host-context'.

    // host
    
    @Component({
      selector: 'my-app',
      template: `
        <div class="styled-component">
          <hostcontext-styling></hostcontext-styling>
        </div>
      `,
    })

    In the host component, we have 'styled-component' class, we want to apply some css to it from the child component:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'hostcontext-styling',
      template: `
        <div>
          I'm a div that wants to be styled
        </div>
      `,
      styles: [
        `
          /* apply a border if any of our ancestors has .styled-component applied */
          :host-context(.styled-component) {
            border: 1px solid gray;
            display:block;
          }
        `
      ]
    })
    export class HostContextStylingComponent {
    }

    Now if we want to style its child component, we can use '::ng-deep':

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'hostcontext-styling',
      template: `
        <div>
          I'm a div that wants to be styled
        </div>
        <child-component></child-component>
      `,
      styles: [
        `
          /* apply a border if any of our ancestors has .styled-component applied */
          :host-context(.styled-component) {
            border: 1px solid gray;
            display:block;
          }
          
          :host ::ng-deep p {
            background-color: yellow;
          }
        `
      ]
    })
    export class HostContextStylingComponent {
    }
  • 相关阅读:
    Java 垃圾回收
    位运算符
    VBA中练习ADO:ActiveX Data Object
    使用VB6制作RTD函数
    VBA中使用JavaScript脚本语言解析JSON数据
    VB洗牌算法产生随机数组
    关于VB里判断逻辑的说明
    在VB中动态执行VBS代码,可操控窗体控件
    VB使用API进行RC4加密解密(MD5密钥)
    VB输出数据到EXCEL
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7599728.html
Copyright © 2011-2022 走看看