0

My angular 7 application, I have the scrolling to see the products from left to right and right to left using this. Scrolling

Each image has the button when ever it is clicked, I have applied the new CSS in typescript (component.ts) as below.

this.appendToSimilar.nativeElement.appendChild(document.getElementById(item.imageId).cloneNode()).classList.add('similar-floating');

Here, I need to pass the inline css instead of class name.

Expectation

this.appendToSimilar.nativeElement.appendChild(document.getElementById(item.imageId).cloneNode()).classList.add('.cssClassName{width:50px, hight: 100px}');

I am sure that .add can be used to add the class name. Anyone help me to add the inline css for above scenario.

4 Answers 4

1

What about using ngStyle

customeStyle = 
{
  'color': 'red'
}

;

  <p [ngStyle]="customeStyle">asdasd</p>
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of querying the DOM, you can use Angular Style binding

Template

<h1 [style.color]="isHoussein() ? 'red': 'black'">{{firstname}}</h1>
Name: <input type="text" [(ngModel)]="firstname">
<button (click)="changeName()">Change Name</button>

Component.ts

export class MyApp {
  firstname: string = 'Jimmy';

  changeName() {
    this.firstname = 'Houssein';
  }

  isHoussein() {
    return this.firstname === 'Houssein';
  }
}

2 Comments

I need to pass the inline css from component instead of the passing class this.appendToSimilar.nativeElement.appendChild(document.getElementById(item.imageId).cloneNode()).classList.add('similar-floating');
ngStyle is an attribute directive that updates styles inline only.
0

Inject Renderer2, and ElementRef in your component, and add the class by using the renderer, and the nativeElement of elementRef (if you want to do something on the dom-element on your component)

constructor(renderer: Renderer2, elementRef: ElementRef) {
    // TODO, create, clone or add classes to the element, or some other element.
} 

Comments

0

You can add inline css from component.ts in this way:

this.appendToSimilar.nativeElement.appendChild(document.getElementById(item.imageId).cloneNode()).style.width = '50px';


this.appendToSimilar.nativeElement.appendChild(document.getElementById(item.imageId).cloneNode()).style.height = '100px';

Comments

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.