2

Say inside my.component.html

<nested-component [class]="nestedClass"></nested-component>

Then when I want to use my component, I want to specify both their style classes:

<my-component class="my-component-style" nestedClass="nested-component-style"></my-component>

The above did not work (i.e. the nested-component did not get assigned the style class .nested-component-style). How would you fix the code above? Other workarounds are welcome.

user1589188
  • 5,316
  • 17
  • 67
  • 130

3 Answers3

3

Use

/deep/ .nested-component-style 

or

>>> .nested-component-style 

in the parent component to have it applied to it's children.

Or to be safer, put your .nested-component-style in styles.css in app level to have it globally defined.

Beware of lack of browser support of /deep/ and >>> selectors.

More info here.

Uğur Dinç
  • 2,415
  • 1
  • 18
  • 25
1

This is correct. just add a @Input variable in your child component just link

  @Input() nestedClass = '';
Imran
  • 3,031
  • 4
  • 25
  • 41
  • @user1589188 I just paste your code in my project and I see the class like `

    Hello

    ` in my child component html
    – Imran Jan 08 '18 at 04:09
  • Yes it sees the style class name, but for some reason the style classes has a selector appended to them, like "my-component-style[_ngcontent-c5]" and "nested-component-style[_ngcontent-c5]". But is generated with "_ngcontent-c8", note the last digit different, this caused the style class failed to be applied to the nested component. – user1589188 Jan 08 '18 at 04:13
1

my.component.html

<nested-component class="nestedClass"></nested-component>

And inside my.component.ts

@Input() nestedClass: string;

and in nexted.component.html

<my-component class="my-component-style" [nestedClass]="'nested-component-style'"></my-component>
Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116
  • Yes this is what I have, it sees the style class name, but for some reason the style classes has a selector appended to them, like "my-component-style[_ngcontent-c5]" and "nested-component-style[_ngcontent-c5]". But is generated with "_ngcontent-c8", note the last digit different, this caused the style class failed to be applied to the nested component. – user1589188 Jan 08 '18 at 04:14
  • Can you have a look again? I don't think thats what you have though. You have `[class]` while mine is `class` and `[nestedClass]` . These are important. – Nuru Salihu Jan 08 '18 at 04:18
  • Yeah, tried both, same. But @dexter above gave a working solution! – user1589188 Jan 08 '18 at 04:24
  • 1
    Ok Cool. Another solution you can try is appending everything as HTML using safeHTML pipe you may have a look @ the answer Here – Nuru Salihu Jan 08 '18 at 04:26