Frontendroom
Tools
Categories
ShortsAbout
Posted on: February 19, 2023|Amrish Kushwaha

How to apply rendered property of a div or element into another div or element in angular

How to apply rendered property of a div or element into another div or element in angular
  1. x
    1. Now Playing
      Up NextHow to Create a Birdsmouth Joint
    2. Now Playing
      Up NextHow To Create An Image Hover Effect With CSS
    3. Now Playing
      Up NextHow To Enable & Disable Screen Shake In Dota 2
    4. Now Playing
      Up NextHow to Make Change Background Border and Box Shadow of Div when Hovered in HTML5 & CSS3
    5. Now Playing
      Up NextParts of a Spinning Reel and How They Work
    6. Now Playing
      Up NextModelling an SR Flip Flip as a Finite State Machine with Python
    7. Now Playing
      Up NextHow to Turn On And Use Striking Face Effect on TikTok
    8. Now Playing
      Up NextAngular Material Button
    9. Now Playing
      Up NextHow to Make a Crochet Chain - (Left Hand) Beginner Course: Lesson #5
    10. Now Playing
      Up NextHow to Change a Spinning Reel From Right to Left Handed by Flipping the Reel Handle
    11. Now Playing
      Up NextHow to make the Tunisian Cinch Bag #tunisiancrochet
    12. Now Playing
      Up NextHow to Create an Angular Project with Vite: Step-by-Step Guide for 2024
    13. Now Playing
      Up NextHow to Make Div Shape Hover Effect in Pure HTML5 & CSS3
    14. Now Playing
      Up NextAnimated Loading Spinner Tutorial
    15. Now Playing
      Up NextCSS Flip Card with Hover Effect __ Vertical Rotating Card
    16. Now Playing
      Up NextAngular ngx-datatable Tutorial to Render Data in Table With Sorting & Pagination in Browser Using TS
    17. Now Playing
      Up NextHow to create 3D effect in Canva_ a person on top of text _ Canva Tutorial
    18. Now Playing
      Up NextHow to Use Animation Painter in PowerPoint Tutorial
    19. Now Playing
      Up NextHow to Dress a Grinding Wheel _ 3 easy ways
    20. Now Playing
      Up NextHtml Css using Angular Part 2
x

Problem: how to apply rendered property of a div into another div

Sometimes we want to use the rendered property of an HTML element such as height, width, etc.. to set the property in another HTML element.

Solution: use ngAfterViewChecked lifecycle

This can be easily done in Angular using lifecycle hook ngAfterViewChecked.

Here is the proper solution:

demo.component.html

<div #div1 class="div1">
<p>Div1</p>
</div>
<div #div2 class="div2">
<p>Div2</p>
</div>

demo.component.css

.div1 {
background-color: lightblue;
height: 100px;
}
@media screen and (max-width: 767px) {
.div1 {
height: 500px;
}
}
.div2 {
background-color: lightgreen;
}

demo.component.ts

import {
Component,
OnInit,
ViewChild,
Renderer2,
AfterViewChecked,
HostListener,
} from "@angular/core"
@Component({
selector: "app-demo",
templateUrl: "./demo.component.html",
styleUrls: ["./demo.component.css"],
})
export class DemoComponent implements OnInit, AfterViewChecked {
@ViewChild("div1") div1
@ViewChild("div2") div2
constructor(private renderer: Renderer2) {}
ngOnInit() {}
@HostListener("window:resize", [])
onResize() {
this.setDivHeight()
}
ngAfterViewChecked() {
this.setDivHeight()
}
setDivHeight() {
let height = `${this.div1.nativeElement.offsetHeight}px`
this.renderer.setStyle(this.div2.nativeElement, "height", height)
}
}

Working Example

https://stackblitz.com/edit/set-rendered-height-of-div-to-another-div

I hope that you like the article.

Keep coding and keep learning and keep building stuffs...

About author:

Amrish Kushwaha

Amrish Kushwaha

I am Amrish Kushwaha. Software Engineer, Maker, Writer. I am currently focused on frontend development with curiosity of building end to end software system. Currently I am working at Rafay Systems. I love to write as well as build side projects when I am free.

Related category articles:

How to deploy an angular app on GitHub Pages using GitHub Actions

How to deploy an angular app on GitHub Pages using GitHub Actions

Read
X
Current Time 0:00
Duration 0:00
Remaining Time 0:00
1x
    • captions off, selected