## Angular 中的 ::ng-deep 與 :host::ng-deep
在 Angular 中,樣式的封裝性對於保持應用的整體風格和避免樣式衝突至關重要。本篇將介紹 `::ng-deep` 和 `:host::ng-deep` 這兩個常用的選擇器,並解釋它們如何影響樣式的作用域。
### 程式碼範例
以下是一個簡單的範例,展示了如何在 Angular 組件中使用這些選擇器。
#### templete.component.html
``` html
<!-- 基本 HTML 結構 -->
<div class="cls">normal</div>
<div class="cls-ng-deep">ng deep</div>
<div class="cls-host-ng-deep">host ng deep</div>
```
#### templete.component.scss
``` scss
// 普通樣式,僅作用於此組件
.cls {
background-color: rgb(250, 235, 219);
}
// ::ng-deep 選擇器,影響全局樣式
::ng-deep .cls-ng-deep {
background-color: rgb(231, 184, 122);
}
// :host::ng-deep 組合,限制作用域於宿主元素及其子元素
:host::ng-deep .cls-host-ng-deep {
background-color: rgb(226, 154, 61);
}
```
### 實際在瀏覽器中的產出
#### DOM
``` html
<!-- Angular 生成的 DOM 節點 -->
<template _nghost-ng-c3794340225="">
<div _ngcontent-ng-c3794340225="" class="cls">normal</div>
<div _ngcontent-ng-c3794340225="" class="cls-ng-deep">ng deep</div>
<div _ngcontent-ng-c3794340225="" class="cls-host-ng-deep">host ng deep</div>
</template>
```
#### 樣式解析
##### `cls`
此選擇器僅作用於特定組件,通過 Angular 自動生成的 component ID 以及屬性選擇器實現。
``` css
.cls[_ngcontent-ng-c3794340225] {
background-color: #faebd7;
}
```
##### `::ng-deep .cls-ng-deep`
移除屬性選擇器後,此規則影響全局樣式。
``` css
.cls-ng-deep {
background-color: #e7b87a;
}
```
##### `:host::ng-deep .cls-host-ng-deep`
此規則將作用域限定於宿主元素及其子元素,避免全局影響。
``` css
[_nghost-ng-c3794340225] .cls-host-ng-deep {
background-color: #e29a3d;
}
```
### 特殊選擇器 `:host`
在 Angular 中,如果您想在組件的 SCSS 樣式表中針對該組件本身應用樣式,可以使用 Angular 提供的特殊選擇器 `:host`。`:host` 選擇器允許您撰寫樣式規則,這些規則僅應用於組件的宿主元素(即組件自己的元素)。
例如,假設您有一個名為 `my-component` 的 Angular 組件,您可以在其 SCSS 檔案中這樣使用 `:host` 選擇器:
```scss
:host {
display: block;
margin: 10px;
padding: 20px;
background-color: lightblue;
}
```
這些樣式將直接應用於 `my-component` 組件的宿主元素上。
此外,`:host` 選擇器還支援使用條件,這意味著您可以根據宿主元素上的某些條件來應用樣式。例如,您可以針對添加了特定類別的宿主元素進行樣式定制:
```scss
:host(.special-class) {
background-color: green;
}
```
在這個例子中,只有當宿主元素有 `special-class` 類別時,背景顏色才會變成綠色。
使用 `:host` 選擇器是 Angular 中設定組件特定樣式的一種標準做法,它有助於保持樣式的封裝性,並確保樣式規則不會意外影響到其他元素。
### 補充資料與注意事項
- CSS 選擇器(CSS Selectors)參考資源:
- [選擇器種類列表](https://www.w3schools.com/cssref/css_selectors.php)
- [屬性選擇器介紹(Attribute Selectors)](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Attribute_selectors)
- 注意:雖然 `::ng-deep` 提供了強大的風格穿透能力,但需謹慎使用,以免造成不必要的全局樣式影響。
**選擇器種類列表 (w3schools)**
| Selector | Example | Example description |
| -------- | -------- | -------- |
| .class | .intro | Selects all elements with class="intro" |
| .class1.class2 | .name1.name2 | Selects all elements with both name1 and name2 set within its class attribute |
| .class1 .class2 | .name1 .name2 | Selects all elements with name2 that is a descendant of an element with name1 |
| #id | #firstname | Selects the element with id="firstname" |
| * | * | Selects all elements |
| element | p | Selects all <p> elements |
| element.class | p.intro | Selects all <p> elements with class="intro" |
| element,element | div, p | Selects all <div> elements and all <p> elements |
| element element | div p | Selects all <p> elements inside <div> elements |
| element>element | div > p | Selects all <p> elements where the parent is a <div> element |
| element+element | div + p | Selects the first <p> element that is placed immediately after <div> elements |
| element1~element2 | p ~ ul | Selects every <ul> element that is preceded by a <p> element |
| [attribute] | [target] | Selects all elements with a target attribute |
| [attribute=value] | [target="_blank"] | Selects all elements with target="_blank" |
| [attribute~=value] | [title~="flower"] | Selects all elements with a title attribute containing the word "flower" |
| [attribute|=value] | [lang|="en"] | Selects all elements with a lang attribute value equal to "en" or starting with "en-" |
| [attribute^=value] | a[href^="https"] | Selects every <a> element whose href attribute value begins with "https" |
| [attribute$=value] | a[href$=".pdf"] | Selects every <a> element whose href attribute value ends with ".pdf" |
| [attribute*=value] | a[href*="w3schools"] | Selects every <a> element whose href attribute value contains the substring "w3schools" |
| :active | a:active | Selects the active link |
| ::after | p::after | Insert something after the content of each <p> element |
| ::before | p::before | Insert something before the content of each <p> element |
| :checked | input:checked | Selects every checked <input> element |
| :default | input:default | Selects the default <input> element |
| :disabled | input:disabled | Selects every disabled <input> element |
| :empty | p:empty | Selects every <p> element that has no children (including text nodes) |
| :enabled | input:enabled | Selects every enabled <input> element |
| :first-child | p:first-child | Selects every <p> element that is the first child of its parent |
| ::first-letter | p::first-letter | Selects the first letter of every <p> element |
| ::first-line | p::first-line | Selects the first line of every <p> element |
| :first-of-type | p:first-of-type | Selects every <p> element that is the first <p> element of its parent |
| :focus | input:focus | Selects the input element which has focus |
| :fullscreen | :fullscreen | Selects the element that is in full-screen mode |
| :hover | a:hover | Selects links on mouse over |
| :in-range | input:in-range | Selects input elements with a value within a specified range |
| :indeterminate | input:indeterminate | Selects input elements that are in an indeterminate state |
| :invalid | input:invalid | Selects all input elements with an invalid value |
| :lang(language) | p:lang(it) | Selects every <p> element with a lang attribute equal to "it" (Italian) |
| :last-child | p:last-child | Selects every <p> element that is the last child of its parent |
| :last-of-type | p:last-of-type | Selects every <p> element that is the last <p> element of its parent |
| :link | a:link | Selects all unvisited links |
| ::marker | ::marker | Selects the markers of list items |
| :not(selector) | :not(p) | Selects every element that is not a <p> element |
| :nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
| :nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second child of its parent, counting from the last child |
| :nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second <p> element of its parent, counting from the last child |
| :nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
| :only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
| :only-child | p:only-child | Selects every <p> element that is the only child of its parent |
| :optional | input:optional | Selects input elements with no "required" attribute |
| :out-of-range | input:out-of-range | Selects input elements with a value outside a specified range |
| ::placeholder | input::placeholder | Selects input elements with the "placeholder" attribute specified |
| :read-only | input:read-only | Selects input elements with the "readonly" attribute specified |
| :read-write | input:read-write | Selects input elements with the "readonly" attribute NOT specified |
| :required | input:required | Selects input elements with the "required" attribute specified |
| :root | :root | Selects the document's root element |
| ::selection | ::selection | Selects the portion of an element that is selected by a user |
| :target | #news:target | Selects the current active #news element (clicked on a URL containing that anchor name) |
| :valid | input:valid | Selects all input elements with a valid value |
|:visited | a:visited | Selects all visited links |