How to pass value from ts file to scss file in same component in angular 2+?

只谈情不闲聊 提交于 2019-12-12 22:18:40

问题


This is my component

export class myComponent implements onInit {

private outageCount;

ngOnInit(){

    ....subscribe((data) => {
    this.outageCount = Object.keys(data).length;

})
}

I need to pass the outageCount to my css before CONTENT

:host ::ng-deep app-settings-panel {
    &:before{
        //content:"4";
        content:attr(outageCount);
        background: #941313;
        position: absolute;
        top: 3px;
        left: 22px;
        border-radius: 50%;
        height: 13px;
        width: 13px;
        border: 1px solid #ffffff;
        color: white;
        font-size: 8px;
        text-align: center;
        line-height: 13px;
    }

How can i pass the outageCount value from my component to css :before content.

Any suggestions would be appreciated!


回答1:


try using :

document.documentElement.style.setProperty('--contentvalue', this.outageCount);

css

:root {
    --contentvalue: 4;
}

:host ::ng-deep app-settings-panel {
    &:before{
        //content:"4";
        content:attr(--contentvalue);
        background: #941313;
        position: absolute;
        top: 3px;
        left: 22px;
        border-radius: 50%;
        height: 13px;
        width: 13px;
        border: 1px solid #ffffff;
        color: white;
        font-size: 8px;
        text-align: center;
        line-height: 13px;
    }



回答2:


I'm passing this as attribute in my html like below

 [attr.outage-count]="outageCount"

I css, i updated like this

   :host ::ng-deep app-settings-panel {
        &:before{
            content: attr(outage-count);
            ......
        }
    }

This works for me!! Thanks for all those who tried helping me!



来源:https://stackoverflow.com/questions/49380916/how-to-pass-value-from-ts-file-to-scss-file-in-same-component-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!