39

I am using Angular 5.2 in my project and new to the angular framework. My component html is looking like this:-

enter image description here

I want to add the style="top:200px;" dynamically to the highlighted element with class="app-alerts" in the above screenshot in the angular ts code. The div element with class "app-alerts" get added to the DOM on the render.

Please suggest the code changes.

13
  • is this going to be based on some condition? Commented Jun 18, 2018 at 11:14
  • There is no condition at the moment. Please suggest the code changes. Commented Jun 18, 2018 at 11:15
  • 2
    you can directly use document.querySelector('app-alerts').style="top:200px" Commented Jun 18, 2018 at 11:17
  • Which event would be appropriate for it ? ngAfterViewInit Commented Jun 18, 2018 at 11:19
  • if it is going to be a child component then use ngAfterViewInit or ngAfterContentInit Commented Jun 18, 2018 at 11:20

4 Answers 4

104

I'm super late to this party, but NO! DO NOT USE DOCUMENT.QUERYSELECTOR. That is NOT how you style an Angular DOM element!

<div [ngStyle]="{'top.px': divStyle}">

Then in your component, you have

ngAfterViewInit() {
  this.divStyle = 200; 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you not do <div [style.top]="'200px'"> ?
@Ben You can even do <div [style.top.px]="variableTopHeight">! If you wanted to inline the value, you could <div [style.top.px]="'200'">
looks neat. but how you will apply style to a class which is present in a third party control dynamically in Angular way?
40

As per our comments, You should be using document.querySelector after a life cycle hook

ngAfterViewInit() {
    (document.querySelector('.app-alerts') as HTMLElement).style.top = '150px';
}

6 Comments

When you need to reach a native element, consider using "Template reference variables" angular.io/guide/… . For specific dynamic style, prefer @chris-stanley anwser
This is not solution for angular, thus the down vote.
No, this is a really bad practice
If this is bad practice then how we can apply style to a class which is present in a third party control dynamically in Angular way?
Also use of document makes this not SSR compatible
|
1

I came across a page that has good explanation and sample code to add the style dynamically in Angular 2+. Attach the style dynamically in Angular

Comments

1

I had the same problem, i needed to scroll a div when user scroll a page, and solved my problem with the code below. At the component where you want to capture the scroll:

import { HostListener } from '@angular/core';

@ViewChild('curtain') divCurtain: ElementRef;

export class ComponentX {
    @HostListener('window:scroll', ['$event']) onScrollEvent($event) {
        console.log(window.pageYOffset);
        this.divCurtain.nativeElement.style.top = window.pageYOffset.toString().concat('px');
    }

    ngOnInit(): void { }
}

Only this, i did not create any directive or other code. This HostListener is executed every time the user scroll the page, and i get the window.pageYOffset to send to my div.

I hope it helps.

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.