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 {
    }
  • 相关阅读:
    call 与 apply
    react-router
    阻止IOS上的弹性滚动
    React规范
    sessionStorage 、localStorage 和 cookie 对比区分
    显式Intent 和隐式 Intent 的区别
    Activity之间传递数据的方式及常见问题总结
    Android横竖屏切换生命周期变化
    String、StringBuilder、StringBuffer 区别
    内存泄漏
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7599728.html
Copyright © 2011-2022 走看看