1

I want to add a class to my component when I click a button. So I do it with Renderer2 as following:

this.renderer.addClass(this.elRef.nativeElement, 'photo-removing');

The class only applies when I add the css class in a global css file. It doesn't work when I add it to the component css file. Any idea why does this happen?

Update: Here is my component html:

<div *ngIf="imgMainUrl">
    <img
        [src]="imgMainUrl"
        [style.width.px]="width" [style.height.px]="height"
        [ngClass]="{ 'img-radius' : rounded }"
    >
    <i
        [pTooltip]="tooltipTxt" [tooltipPosition]="tooltipPosition"
        class="icon-close close-btn"
        (click)="removePhoto()">
    </i>
</div>

And css:

.photo-removing {
    opacity: 0.4;
    border:1px solid red;
}
5
  • ngClass is a much more Angular way of doing this. On your component you would just have some logic that applies the class when the button is clicked. I can provide a more detailed answer if you'd rather do it this way. Commented Jul 9, 2019 at 21:19
  • we'd need to see more about the template structure and the css itself to understand why the it's only working when defined in the global css context. Commented Jul 9, 2019 at 21:31
  • @bryan60 Thank you. Please see the updated part. Commented Jul 10, 2019 at 10:56
  • And you’re trying to put the css in this component style sheet? Not a parent component? Commented Jul 10, 2019 at 12:26
  • @bryan60 Yes exactly. And one more thing, this component is a shared module's component which will be used in main modules. Commented Jul 10, 2019 at 13:06

3 Answers 3

1

You can use [ngClass] with *ngIf instead

In .html:

<button (click)="buttonClicked()"></button>

<div [ngClass]={checker === true ? 'photo-removing' : ''}></div>

And in .ts file:

export class Home{

  checker:boolean = false;
  constructor(){}


  buttonClicked() {
   this.checker = !this.checker;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I know I can use ngClass, but I just want to know the reason.
1

This is a perfect example of when to use [ngClass] directive.

If you're simply using the button to toggle some variable, you can do this...

In your component.ts file:

public clicked: boolean = false;

click() {
     this.clicked ? false : true;
}

In your component.html file:

<button (click)="click()">Set to {{!clicked}}</button>

<div [ngClass]="{'your-class' : clicked}"></div>

If you really want to use a renderer, the you need to set this code in your angular app:

encapsulation: ViewEncapsulation.None.

By setting this value to None, you are telling Angular not to encapsulate your views. That way, the default scoping rules, isolations, and protections for styling won't apply. So, Angular adds the CSS to the global styles. This is essentially the same as pasting the component's styles into the HTML.

https://angular.io/api/core/ViewEncapsulation

2 Comments

Thanks, I know I can use ngClass, but I just want to know the reason.
Please see my updated answer ^ it has to do with view encapsulation.
1

Try:-

Stackblitz Link:- https://stackblitz.com/edit/renderer2-example-2-wdz4og?file=src/app/app.component.css

.html

<div #test *ngIf="imgMainUrl">
  test
    <img
        [src]="imgMainUrl"
        [style.width.px]="width" [style.height.px]="height"
        [ngClass]="{ 'img-radius' : rounded }"
    >
<button type="button" (click)="addClass()">asasas</button>

</div>

.ts

  @ViewChild('test') test: ElementRef;
  imgMainUrl = true;
  constructor(private renderer: Renderer2) { }
  addClass() {
    this.renderer.addClass(this.test.nativeElement, 'photo-removing');
  }

2 Comments

Use #test @ViewChild('test') test: ElementRef;
I will check it ASAP.

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.