Table of Contents
1. :host
2. :: ng-deep
3. :host-context
Home Web Front-end JS Tutorial Angular's :host, :host-context, ::ng-deep selectors

Angular's :host, :host-context, ::ng-deep selectors

May 31, 2022 am 11:08 AM
angular angular.js

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!

Angular's :host, :host-context, ::ng-deep selectors

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.

If there is the following html:

<app-detail></app-detail>
Copy after login

The style of component

app-detail (the style of the entire app-detail) is as follows:

:host {
    display: inline-block;
    background: red;
}
Copy after login

Browser

Elements Select the app-detail element, the Style is as follows:

[_nghost-wtd-c445] {
    display: inline-block;
    background-color: red;
}
Copy after login

As you can see,

: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;
}
Copy after login

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;
}
Copy after login

Like this:

<app-detail class="active"></app-detail>
Copy after login

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;
}
Copy after login

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;
}
Copy after login

3. :host-context

If a certain condition needs to be met before the style can be applied. It looks for CSS classes in the

ancestor nodes of the current component's host element, up to the document's root node. If is 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;
}
Copy after login

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 style

h1{
    color: hotpink;

    :host-context(.active) &{
        color: yellow;
    }
}
Copy after login
For more programming-related knowledge, please visit:

Programming Teaching

! !

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1245
24
Angular learning talks about standalone components (Standalone Component) Angular learning talks about standalone components (Standalone Component) Dec 19, 2022 pm 07:24 PM

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!

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

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

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

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

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

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!

Angular + NG-ZORRO quickly develop a backend system Angular + NG-ZORRO quickly develop a backend system Apr 21, 2022 am 10:45 AM

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!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

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

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

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!

Token-based authentication with Angular and Node Token-based authentication with Angular and Node Sep 01, 2023 pm 02:01 PM

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to

See all articles