View Encapsulation Flashcards

1
Q

What is View Encapsulation

A

In Angular, component CSS styles are encapsulated into the component’s view an don’t affect the rest of the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

View Encapsulation modes

A

These modes control how encapsulation happens on a per component basis:

  • ShadowDom - Uses the browser’s native shadow DOM implementation to attach a shadow DOM to the component’s host element, and then puts the component view inside that shadow DOM.
  • Emulated - Emulates the behavior of shadow DOM by preprocessing and renaming the CSS code to effectively scope the CSS to the component’s view
  • None - Angular does no view encapsulation. Angular adds the CSS to the global styles. The scoping rules, isolations, and protections discussed earlier don’t apply.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to set View Encapsulation

A

Use the encapsulation property in the component metadata:

encapsulation: ViewEncapsulation.ShadowDom

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is _nghost

A

An element that would be a shadow DOM host in native encapsulation has a generated _nghost attribute. This is typically the case for component host elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is _ngcontent

A

An element within a component’s view has a _ngcontent attribute that identifies to which host’s emulated shadow DOM this element belongs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly