0

I'm using two different components A: sea.component.ts and B:sun.component.ts both call a child component in these child I have to load a different css if is called by sea or sun component. Is possible to pass css url as an input in child component as:

 <sun_parent>
     <child [css]="sun_css"></child>
 </parent>

 <sea_parent>
     <child [css]="sun_css"></child>
 </sea_parent>
1
  • 1
    Yes. You can create an object to pass as an input, as use the object with ngStyle angular.io/api/common/NgStyle Commented Jul 24, 2018 at 19:35

1 Answer 1

1

There could be a way maybe but here's a suggestion: Both of your component calls the same child component, so you can take advantage of it by using an id on the parent div, like this:

Here's an example:

https://stackblitz.com/edit/reuseable-component

<parent id="iAmSeaParent">
    <child></child>
</parent>

<parent id="iAmSunParent">
    <child></child>
</parent>

In your style.css

#iAmSeaParent childClassOrIdOrTag {
    background: yellow;
}

#iAmSunParent childClassOrIdOrTag {
    background: yellow;
}
Sign up to request clarification or add additional context in comments.

4 Comments

As far as I'm aware, this wouldn't work because of view encapsulation
Isn't he looking for something like this: stackblitz.com/edit/angular-d2m7mk or I didn't understand anything?
Sorry, I missed that you were adding it to style.css (which doesn't feature ViewEncapsulation) as opposed to the component's own CSS file
NP. This is why I made a stackbliz demo... because if the answer's not correct then it shouldn't be here and I would have deleted it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.