Angular's :host, :host-context, ::ng-deep selectors
This article will give you an in-depth understanding of several special selectors in angular: host, :host-context, ::ng-deep. I hope it will be helpful to everyone!
1. :host
##:host means selecting the current component. [Related tutorial recommendations: "angular tutorial"]
1.1 Select the host element
Use:host Pseudo-class selector, used to select elements in the component
host element (relative to the elements inside the component template). Without child elements, it is equivalent to selecting the entire
host element.
<app-detail></app-detail>
app-detail (the style of the entire
app-detail) is as follows:
:host { display: inline-block; background: red; }
Elements Select the
app-detail element, the Style is as follows:
[_nghost-wtd-c445] { display: inline-block; background-color: red; }
:host directly acts on
The host element itself
1.2 Select the child elements of the host element
Can be found in:host Add a selector later to select
child elements . For example:
:host h1 Position the
h1 tag within the component view
:host h1 { color:red; }
1.3 Conditionally select the host element
It will only take effect when the host is used as the target and has an active class.:host(.active){ border-width: 3px; }
<app-detail class="active"></app-detail>
2. :: ng-deep
::ng-deep can ignore the nested hierarchical relationship of the intermediate className. Directly find the className you want to modify.When using some third-party components, you need to modify the style of the component. In this case, use.
2.1 From the host element to the current element Then go to all child h3 elements in the DOM, including h3 elements using third-party components in the current component
:host ::ng-deep h3 { font-style: italic; }
2.2 Search for a specific type under a certain type
.card-container ::ng-deep .ant-tabs-card .ant-tabs-content { height: 120px; margin-top: -16px; }
3. :host-context
If a certain condition needs to be met before the style can be applied. It looks for CSS classes in theancestor
nodes of the current component's
host element, up to the document's root node.
Ifis found, the following styles will be applied to the
internal elements of this component.
3.1 Select the element in the component host element:host-context {
color:red;
}
3.2 Target the host with It will only take effect if there is an active classIn the following example, only if a
ancestor element(host element can also be used) has a CSS classtheme-light
, the background-color
style will be applied to all
elements inside
of this component. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>:host-context(.theme-light) h2 {
background-color: #eef;
}</pre><div class="contentsignin">Copy after login</div></div>
3.3 You can add a selector after :host-context to select sub-elements For example:
:host-context h1 Positioning the h1
tag in the component view <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>:host-context h1{
color: hotpink;
}</pre><div class="contentsignin">Copy after login</div></div>
3.4 can be used to determine the internal conditions of a certain styleh1{
color: hotpink;
:host-context(.active) &{
color: yellow;
}
}
The above is the detailed content of Angular's :host, :host-context, ::ng-deep selectors. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This article will take you to continue learning angular and briefly understand the standalone component (Standalone Component) in Angular. I hope it will be helpful to you!

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

Do you know Angular Universal? It can help the website provide better SEO support!

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

This article will share with you an Angular practical experience and learn how to quickly develop a backend system using angualr combined with ng-zorro. I hope it will be helpful to everyone!

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!
