I want to change the 'content' property of a CSS class which is getting applied on Slide-Toggle of angular.
this is my SCSS -
:host .red {
.mat-toggle {
::ng-deep .mat-slide-toggle-bar{
&::after{
content: attr(data-active);;
}
}
}
}
This is how I pass attrs -
<div class="red"
attr.data-active="activeData">
<mat-toggle></mat-toggle>
</div>
If I hard-code the value in CSS then it's working properly, but unable to bind the string dynamically.
Please help.
The issue is that
attr()will not pierce down to the child element to set a value... it only works onselected element.Reference to
MDNthat specifiesselected elementin the description.With this in mind, you would need to set the attribute value into a CSS variable for later use.
Using the following approach from this SO answer may be a viable workaround.
Add
label-attrto be used inCSSvia attribute selector, then setcustom property/CSS variableviastyle="--myLabel:'activeLabel';"In
CSSuse the attribute selector to get a reference to your CSS variable and set it to content viacontent: var(--myLabel).STACKBLITZ
https://stackblitz.com/edit/angular-azgink-dvthvu?file=src/app/slide-toggle-configurable-example.css