in Education by
In my angular project, I have some css variables defined in top level styles.scss file like this. I use these variable at many places to keep the whole theme consistent. :root { --theme-color-1: #f7f7f7; --theme-color-2: #ec4d3b; --theme-color-3: #ffc107; --theme-color-4: #686250; --font-weight: 300 } How can I update values of these variables dynamically from app.component.ts ? And What is the clean way to do this in angular ? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can update them using document.documentElement.style.setProperty('--theme-color-1', '#fff'); If u want to update many values, then create a object this.styles = [ { name: 'primary-dark-5', value: "#111" }, { name: 'primary-dark-7_5', value: "#fff" }, ]; this.styles.forEach(data => { document.documentElement.style.setProperty(`--${data.name}`, data.value); }); The main thing here is document.documentElement.style.setProperty. This line allows you to access the root element (HTML tag) and assigns/overrides the style values. Note that the names of the variables should match at both places(css and js files) if you don't want to use document API, then you can use inline styles on HTML tag directly const styleObject = {}; this.styles.forEach(data => { styleObject[`--${data.name}`] = data.value; }); Then In your template file using ngStyle (https://angular.io/api/common/NgStyle) Set a collection of style values using an expression that returns key-value pairs. ... ... //not sure about quotes syntax Above methods do the same thing, "Update root element values" but in a different way. When you used :root, the styles automatically got attached to HTML tag

Related questions

0 votes
    How can I apply my div position x value to a variable? And how to iterate it in a for loop? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I've noticed that the chart object has a fontSize property, but just changing it does nothing, even with ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I would like to dynamically assign a class to a specific cell in a table. An object in "cols" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 21, 2022 in Education by JackTerrance
0 votes
    I would like to dynamically assign a class to a specific cell in a table. An object in "cols" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 15, 2022 in Education by JackTerrance
0 votes
    I would like to dynamically assign a class to a specific cell in a table. An object in "cols" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
0 votes
    I'm trying to use string interpolation on my variable to reference another variable: // Set up variable and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I use the vendor time picker component. Unfortunately, it uses rem units for most of its sizes, but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I'm using Zend Framework and I have a form input button with a style that I defined. I need to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I'm using Zend Framework and I have a form input button with a style that I defined. I need to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    It seems using getters inside templates causes Angular change detection to go into a loop (getter gets ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have been developing a webpage which uses n number of dropdownlists which are binding dynamically inside a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    In my Android Application I want to create a bunch of ConstraintLayouts where each has for example an ImageView and a TextView ... .LayoutParams cparams.setMargins(4, 4, 4, 4) //...
asked May 7, 2022 in Education by JackTerrance
0 votes
    My issue is basically this: I have a bunch of svg images all stored in a .js file like this ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    My issue is basically this: I have a bunch of svg images all stored in a .js file like this ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...