1

is there a way to define that when my condition is not true, my component gets other attribute styles in Angular? i want to define my main css file.

i want something like this:

example (s-help.component.html): (not correct)

<div *ngIf="visible; else class="test" " >
   <div class="s-help-content "><span [translate]="text"></span></div>
</div>

css

.s-help{
    background-repeat: no-repeat;
    background-position: center left calc(0.37em + 0.37rem);
    background-size: calc(1em + 1rem) calc(1em + 1rem);
    border: 2px solid $warning !important;
    border-radius: 4px;
    display: block !important;   
    color: $gray-700;
    min-width: 220px;
    white-space: normal !important;
    padding-left: 5px !important;
    padding-right: 5px !important;

}
.s-help-content {
    padding-top: 6px;
    padding-bottom: 6px;
    padding-left: 45px;
    padding-right: 6px;
}

.test {
    display: none;
}

component used in code

<s-help [visible]="true" [ngClass]="'s-help'" [text]="'INFOTEXT.101' | translate"></s-help>

my issue is that the border of the component is visible all time, even if the condition is false

to make it more clear: https://stackblitz.com/edit/angular-ivy-etrn9z?file=src%2Fapp%2Fcomp%2Fcomp.component.html i dont want the border under the first info text

1
  • 1
    Take a look at NgClass and NgStyle Commented Aug 22, 2022 at 18:01

1 Answer 1

1

Is this what you need? ngClass will take a javascript expression as an input, here you can conditionally add the class or remove if needed!

<div *ngIf="visible" [ngClass]="!visible ? 'test' : ''" >
   <div class="s-help-content "><span [translate]="text"></span></div>
</div>

updated stackblitz

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

10 Comments

this was a good intention, but i dont know whats the problem, there is still the border! in my code i add the component, i edited my example.
@du7ri please share a stackblitz! this question is being asked a lot of times, if provided maybe ur problem will be resolved!\
@du7ri Thank you so much, is this the expected output stackblitz
@du7ri That is what my stackblitz is doing check here
|

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.