This is not how you do it in Angular! This is how you would do it:
In your CarItemComponent add a vairable for the font-size for example:
fontSize: number;
In your addStyle() set this number to the font size you want to use and in your template add this to the element which's font-size you want to change:
[style.font-size.px]="{{ fontSize }}"
Answer from tommueller on Stack OverflowThis is not how you do it in Angular! This is how you would do it:
In your CarItemComponent add a vairable for the font-size for example:
fontSize: number;
In your addStyle() set this number to the font size you want to use and in your template add this to the element which's font-size you want to change:
[style.font-size.px]="{{ fontSize }}"
This could be useful (Angular 6):
- Change styles in order to look like disabled
- Eliminate the href
https://stackblitz.com/edit/angular-wzgr4h
You can employ ngStyle property for this, something like:
<mat-toolbar [ngStyle]="{'position': div1 ? 'fixed' : 'static' }" *ngIf="div1" >
<mat-toolbar-row>
<div class="input-group has-search">
<input class="form-control py-2 rounded-pill mr-1 pr-5" placeholder="Search">
</div>
</mat-toolbar-row>
</mat-toolbar>
<button (click)="home()></button>
<mat-toolbar>
You can do this several different ways, you can use [ngStyle] which will set all the styles. You can use [class.classname]="true/false" or like I have here just set the style for position directly.
However the *ngIf should completely remove the element, so your styling issue you are seeing could be unrelated.
<mat-toolbar [style.position]="div1 ? 'fixed' : 'static'" *ngIf="div1" >
<mat-toolbar-row>
<div class="input-group has-search">
<input class="form-control py-2 rounded-pill mr-1 pr-5" placeholder="Search">
</div>
</mat-toolbar-row>
</mat-toolbar>
<button (click)="home()></button>
I have a way to do this using a styles service based on https://en.programqa.com/question/52907585/
Within Global.SCSS
@mixin ExportVariables($map, $prefix: null) {
$mapPrefix: "--#{$prefix}";
@if ($prefix){
$mapPrefix: "#{$mapPrefix}-";
}
body {
@each $name, $value in $map {
#{$mapPrefix}#{$name}: $value;
}
}
}
--idle-state: #29ABE2;
// Import each of these in the theme service
$stateSCSS:(
idle: var(--idle-state),
);
@include ExportVariables($stateSCSS, 'stateSCSS');
In the Service
const bodyStyles = window.getComputedStyle(document.body);
this.stateSCSS = {
idle: bodyStyles.getPropertyValue('--stateSCSS-idle'),
};
I think this answers your questions: access SASS values ($colors from variables.scss) in Typescript (Angular2 ionic2)
TLDR:
Unfortunately, there is no way to access SASS variable directly from typescript/javascript code. However, we can make a workaround to access those variables.
You can view the workaround in the post mentioned above
This is one way you can add dynamic classes
<p [class.boldClass]="isbold"
[class.italicsClass]="isItalic">testing content </p>
The issue is with your JSON, when you declare a JSON, the properties of it shouldn't be with the "=", try to use something like:
addClasses(){
let classes={
boldClass: this.isbold,
italicsClass: this.isItalic
};
return classes;
}
There was a pending "," after this.isbold, and also, you had this.Italic, and it should be this.isItalic.
try to convert your selection as HTMLElement
const element = <HTMLElement> document.getElementsByClassName('cal-meeting')[0];
then use :
element.style.display = 'none';
also you can use *ngif for remove it but if you want to use javascript's function to change style you should convert it to HTMLElement but you can use angular [ngStyle]="{'property': expression}" for changing style like :
<li class="cal-meeting" [ngStyle]="{'display': !this.display_meeting ? 'none': 'block'}">meeting title</li>
or you can add class for example if you have class like :
.d-none: {display: none}
.d-block: {display: block}
you can use it in typescript like:
export class MyComponent implements OnInit {
hidingClass: string = '';
toggleCal(toggle_items) {
if (toggle_items === 'meeting') {
this.display_meeting = !this.display_meeting;
if ( this.display_meeting ) {
this.hidingClass = 'd-block'
} else {
this.hidingClass = 'd-none'
}
}
// do the same thing if toggle_items === 'event'
}
just use it in your html element:
<li class="cal-meeting" [ngClass]="hidingClass">meeting title</li>
If it's just toggle the view, why you did not put something simple like this
<li class="cal-meeting" *ngIf="this.display_meeting">meeting title</li>
You can directly add class to the element, created in the TypeScript file.
TypeScript Code
test.textContent = "Created in TS";
test.setAttribute("class","MyClass");
container.append(test);
CSS
.MyClass {
color: green !important;
}
Why not work:
The way Angular manage the .css (to isolate the .css of each element) is adding a prefix in .css. So if you check the .html using F12 in your navigator
You see a .css like
p[_ngcontent-ng-c474887881] {
color: red;
}
And a .html like
<div _ngcontent-ng-c474887881="" class="testContainer">
<p _ngcontent-ng-c474887881="">Created in HTML</p>
<p>Created in TS</p>
</div>
(the _ng-content-ng-c474887881] can be another one, it's only to explain me.
So, when you create using simple javascript your "divs" has not the attribute _ngcontent-ng-..., and not get the .css
Solutions:
You can use a global .css (adding in styles.css or using ViewEncapsulation.None), but in this case the .css is applied to all the application, and to make the .css it's only to the element created you need add a class to your element to be more specific the .css
The another way is use Renderer2
const container = document.querySelector('.testContainer') //(*) const div = this.renderer.createElement('p'); const text = this.renderer.createText('Created in TS by renderer2'); this.renderer.appendChild(div, text); this.renderer.appendChild(container, div);(*) I prefer use a template reference variable and ViewChild,
In your .html
<div #container> <p>Created in HTML</p> <!-- Created in TS--> </div>In .ts
//I use static true because the div is NOT under a *ngIf @ViewChild('container',{static:true}) container!:ElementRef //and use this.renderer.appendChild(this.container.nativeElement, div);
This should work.
this.elementRef.nativeElement.ownerDocument.body.style.setProperty("overflow-y", "hidden", "important");
html:
<h2 #elem>hiper king</h2>
ts:
import { Component, ViewChild, Renderer2, ElementRef, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements AfterViewInit {
title = 'stackApp';
@ViewChild('elem') elem: ElementRef;
constructor(private renderer: Renderer2) { }
ngAfterViewInit() {
this.renderer.setAttribute(this.elem.nativeElement, 'style', 'overflowY: hidden !important');
}
}
Try this
Ref: Either i am using Angular Renerer2 wrong, or it is broken. Can anyone figure this out?