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 {
}

[Angular] Use :host-context and the ::ng-deep selector to apply context-based styling的更多相关文章

  1. [Angular 6] 初学angular,环境全部最新,[ ng serve ] 不能启动,卡在 95% 不动 => 解决方案

    2018.9.7 问题描述: 通过ng serve命令启动angular应用时,卡在95%, ctrl+c 停掉后看到错误内容为找不到ng_modules下的angular模块下的package.js ...

  2. Aplication的意义和生命周期,与Context的关系,以及关于Aplication和Context相关问题的记录和解决办法

    Context详解地址链接: http://blog.csdn.net/qinjuning/article/details/7310620 Application是一个应用中有且仅有一个的全局共享变量 ...

  3. Android深入理解Context(二)Activity和Service的Context创建过程

    前言 上一篇文章我们学习了Context关联类和Application Context的创建过程,这一篇我们接着来学习Activity和Service的Context创建过程.需要注意的是,本篇的知识 ...

  4. 全局获取Context的技巧(再也不要为获取Context而感到烦恼)

    1.Context概念 Context,相信不管是第一天开发Android,还是开发Android的各种老鸟,对于Context的使用一定不陌生~~你在加载资源.启动一个新的Activity.获取系统 ...

  5. java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present

    @Controller@ComponentScan@Configuration@EnableScheduling@EnableAutoConfiguration(exclude={DataSource ...

  6. 前端JS基础知识

    1. 原型 / 构造函数 / 实例 原型(prototype): 一个简单的对象,用于实现对象的 属性继承.可以简单的理解成对象的爹.在 Firefox 和 Chrome 中,每个JavaScript ...

  7. Underscore.js(1.9.1) 封装库

    // Underscore.js 1.9.1// http://underscorejs.org// (c) 2009-2018 Jeremy Ashkenas, DocumentCloud and ...

  8. Istio多集群(1)-多控制面

    Istio多集群(1)-多控制面 参考自官方文档. 目录 Istio多集群(1)-多控制面 复制控制面 要求 在每个集群中部署Istio控制面 配置DNS 配置应用服务 配置用例服务 卸载 FAQ 复 ...

  9. 如何在JavaScript中正确引用某个方法(bind方法的应用)

    在JavaScript中,方法往往涉及到上下文,也就是this,因此往往不能直接引用,就拿最常见的console.log("info…")来说,避免书写冗长的console,直接用 ...

随机推荐

  1. 125.C++输入小结

    #include <iostream> #include <iomanip> #include <cstring> #include <cstdlib> ...

  2. Network Stack‎ : Disk Cache

    Disk Cache 目录 1 Overview 2 External Interface 3 Disk Structure 3.1 Cache Address 3.2 Index File Stru ...

  3. [Chromium文档转载,第006章]Chrome IPC To Mojo IPC Cheat Sheet

    For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎ Chrome IPC To Mojo IPC Cheat Sheet 目录 1 O ...

  4. SpringMVC与SpringBoot返回静态页面遇到的问题

    1.SpringMVC静态页面响应 package com.sv.controller; import org.springframework.stereotype.Controller; impor ...

  5. BZOJ4320 homework

    Description:给定\(n\)个操作,向集合中加入一个数(保证每个数不同)或者查询集合内\(\text{%Y}\)的最小值 Solution:对于小于\(\sqrt{300000}\)的直接暴 ...

  6. FZU 1968 Twinkling lights III

    Twinkling lights III Time Limit: 8000ms Memory Limit: 131072KB This problem will be judged on FZU. O ...

  7. ASP.NET通过JavaScript实现Button 的Enabled=false

    正常代码控制无法满足需求,所以记录一下方法: <input id="Button5" onclick=" CX()" type="button& ...

  8. [Python] Slicing Lists

    In addition to accessing individual elements from a list we can use Python's slicing notation to acc ...

  9. Exception in thread &quot;main&quot; java.lang.UnsupportedClassVersionError: org/apache/ma ven/cli/Maven

    安装maven 中出现例如以下异常: Exception in thread "main" java.lang.UnsupportedClassVersionError: org/ ...

  10. 基于SVM的数据分类预測——意大利葡萄酒种类识别

    update:把程序源代码和数据集也附上http://download.csdn.net/detail/zjccoder/8832699 2015.6.24 --------------------- ...