11

I've seen an angular course telling that :

host-context is used to style elements inside a component, depending on some condition set outside of it.

I've searched the official documentation for it in https://angular.io

but it is not documented

can some one explain the different use cases where I can use this selector for an angular component ?

can some one explain the whole meaning of the -context added to host here ?

without an official documentation, does it mean that when someone give one use case, it mean that it is the only case the thing refers to ?

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
  • 1
    Possible duplicate of [Cannot Understand the Use of :host in components in Angular2](https://stackoverflow.com/questions/42583629/cannot-understand-the-use-of-host-in-components-in-angular2) – Sajeetharan Jan 03 '18 at 04:38
  • @HJEMAI a good explanation of :host and :host-context https://www.youtube.com/watch?v=J5Bvy4KhIs0. – Ajay Jan 03 '18 at 04:59
  • making a duplicate question some seconds after posting it, will let me go in another place to search how i can better style my component ! even if I've already searched. – HDJEMAI Jan 03 '18 at 05:01
  • can some one explain the whole meaning of the `-context` here ? – HDJEMAI Jan 03 '18 at 05:18
  • @HDJEMAI this [link](https://rahulrsingh09.github.io/AngularConcepts/host) might help you – Rahul Singh Jan 03 '18 at 05:28
  • :host-context documentation -> https://angular.io/guide/component-styles#host-context – Luminous Sep 29 '21 at 18:36

1 Answers1

31

This answer explains the difference between host and host-context. Here is an example of host-context usage. Suppose you have a component that wraps an input and this input can be used inside two different components - table and dropdown. When inside a dropdown it should occupy 50% of the width, when in table - 100%. Now if you have these two components selectors defined like this:

<my-dropdown>
<my-table>

Then the styles for the input component can be defined like this:

:host-context(my-dropdown) input { width: 50% }
:host-context(my-table) input { width: 100% }
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488