0

I have a component and it has used in 2 places in application, one for listing and and another in slider, only difference is in CSS, one has larger view and another has smaller and some other CSS changes.

So I am thinking to use same component at both places and use different external style, but for that I need to add CSS url in to Component decorator dynamically.

Is this possible? If yes how?

3
  • Please so your code where component used and also share component Commented Apr 2, 2018 at 4:17
  • I think this answer can help you much. Commented Apr 2, 2018 at 4:22
  • You could also have your component include all CSS, but only use the correct one depending on the context Commented Apr 2, 2018 at 6:39

1 Answer 1

0

I think you are looking for the directive ngStyle and ngClass

If you want to change a single css property, choose ngStyle like this:

<some-element [ngStyle]="{'font-size': styleExp}">...</some-element>

Your styleExp could be a conditional statement like

  isSmall ? 80% : 100%

If you want to apply a style class you need to use ngClass like this:

<some-element [ngClass]="{boolExp ? 'first' :'second'}">...</some-element>    
<some-element [ngClass]="{'first': boolExp }">...</some-element>

your boolExp has to return a boolean value. You can use different combinations for multiple classes.

Use the urls above for reference.

Here is another URL for learning purposes:

Sign up to request clarification or add additional context in comments.

Comments

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.