Linking Angular Template: Connect the height of one container to the height of another

My objective is to dynamically bind the height of an angular component to a div element. I am experimenting with using template reference to access the height.

Is there a way to achieve this in a clean manner? I would like to implement it similar to the code snippet below.

<div [style.height.px]="section1.offsetHeight"></div>
<app-custom-tag #section1>
</app-custom-tag>

The goal is for the height of <div> to match the height of <app-custom-tag>

Using CSS column layout is not an option in this case as these two elements are not positioned together.

Answer №1

It seems like there are a few additional steps you need to take.

If you mention a particular part of the view, you will receive the corresponding class instance. By doing this, you can inject ElementRef to access information about the native element.

Take a look at this example:

foo.component.ts

@Component({
  selector: 'foo',
  template: `foo`,
  styles: [`:host { color: red; display: block; height: 300px; }`]
})
export class FooComp {

  get offsetHeight() {
    return this.el.nativeElement.offsetHeight;
  }

  constructor(private el: ElementRef) {}
}

app.component.ts

@Component({
  selector: 'my-app',
template: `
  <foo #f></foo>
  {{ f.offsetHeight }}

  <br>

  <div style="border: 1px solid black" [style.height.px]="f.offsetHeight"></div>
  
  <br>

  <input #i id="demo" type="text">

  {{ i.id }}
`,
})
export class AppComponent {}

When you mention a DOM element in the view, you will receive that specific DOM element.

Check out the ng-run demo here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for changing text to red when hovering over it in a nested list?

a { color:black; } a:hover { color:red; } <ol> <a href="#"><li>Main1</li></a> <a href="#"><li>Main2</li> <a href="#"><li>Main3 <ol> ...

AJAX response for form validation

I need to validate my contact form when the submit button is clicked. If all fields are valid, I want to display a Processing message using AJAX, followed by a success message with the entered name. The content of my Form is: <form onsubmit="return va ...

Mastering JSON arrays in jQuery

In order to populate a dynatree using a JSON array structure, I may need to consider altering the current structure. The JSON data I have is in response to an ajax call: The JSON data consists of an array of testSteps (0 corresponds to 001016..., 1 corres ...

Submitting a form in React/Javascript: A step-by-step guide

I have a website on Wordpress where I am using the Wp-Polls plugin to vote. How can I submit a post request by accessing a form URL in a React project? Specifically, how can I vote for the "Bad" option with a value of 2? The HTML structure of the WordPre ...

I'm looking for recommendations on where to begin learning about JavaScript touch events for web development. Any suggestions?

Seeking guidance on creating image galleries and content sliders that work with both touch and mouse events. Where should I begin exploring touch event implementation? I'm having difficulty locating official documentation. Are there any jQuery-suppo ...

Delivering compressed files in a React server

Having some trouble serving a gzip compression of my bundle.js file in React. Have tried reducing the size with uglify and dedupe, but only saw a small decrease from 2.9mb to 2.6mb. Using the compression plugin now outputs a gzip file, however, still servi ...

Angular retrieves elements between indexes following ordering

After applying the orderBy on a table, the ordering is lost and the values of $index are from the sorted array rather than the original one. For example, if you have an items array with values ['orange', 'banana','potato',&apo ...

The onClick event's detail property is persisting even after the React component has been replaced

In the onClick event, the value of event.detail indicates the number of clicks, with a double-click having event.detail = 2. It seems that when the React component being clicked is replaced, the event.detail value does not reset. This could be due to Reac ...

What is the proper way to utilize this jQuery function?

My dropdown is being populated with dynamic data through an ajax call within a function. Here is the function I am using: function Banks($selecter, selected) { // my stuff ---- } I initialize the function like this when the document is ready: Banks( ...

JavaScript is throwing an error when clicking on functions and managing the z-index property

I am experiencing difficulties with my website code. I have defined all the necessary functions and CSS, but I keep encountering errors such as "clicked is not defined," even though it is clearly present in the script. The goal of the code is to bring the ...

Method for transferring all items to a new array in AngularJs

There are 2 arrays: $scope.first = [ { fName:'Alex', lName='Doe' }, { fName:'John', lName='S' } ] var second= [ { fName:'Tom', lName='M', email:'<a href="/cdn-cgi/l/email-protectio ...

Programmatically simulating a click on a link within a Windows Universal Windows Platform (U

I am facing an issue with a code that has multiple items with the same href attribute due to it being an external source. I need help figuring out how to programmatically click on a specific link tag using C# within a WebView or by accessing the source d ...

Develop a circular carousel using React JS from scratch, without relying on any third-party library

I am looking to replicate the carousel feature seen on this website. I want to mimic the same functionality without relying on any external libraries. I have found several resources explaining how to achieve this. Most suggest creating duplicate copies o ...

Attempting to render an image onto a canvas and apply Caman.js for image editing purposes

Currently, I have a code snippet that allows me to draw an image onto a canvas. Here is the code: var imageLoader = document.getElementById('imageLoader'); imageLoader.addEventListener('change', handleImage, false); var ...

After passing through JavaScript, my link becomes scrambled

As I'm coding in JavaScript for a website, I've come across a puzzling issue with this string. The troublesome line of code is: var app = "<a onclick='openApp('"+url+"')">"+icon+"</a>" Strangely, when executed, it pr ...

Adjust the form input box height to a different size

Is there a way to adjust the height of the form input box so that the text is positioned close to the top and bottom borders of the box? I attempted to modify the CSS but was unsuccessful. Any suggestions for a Bootstrap CSS solution would be greatly appre ...

Utilize a random file import with the help of React JS, Babel, and ES6

Having trouble displaying a random image using webpack. I have a directory with various images, such as: 1.jpg, 1-cropped.jpg 2.jpg, 2-cropped.jpg I want to fetch a random image from this directory without manually adding a reference for each file. I&apo ...

Should I use Object.assign or define class properties?

Currently in the process of developing an angular application that interacts with the twitch API. The API returns data in various formats, some of which I need to parse and save into specific classes. My main concern is understanding the potential drawbac ...

What causes the difference in behavior of 'flex-grow' between Chrome and Firefox/Edge?

Trying to create a webpage with a fixed height 'header' div followed by a 'content' div below it. The content div contains various other divs with actual page content, generating dynamically through PHP resulting in varying heights for ...

The rating script fails to update when multiple ratings are applied simultaneously

Greetings! I am currently in the process of developing a rating script and encountering some issues. At the moment, the script is somewhat functional -> The problem arises when I open two browsers, load image 1 on both, and rate the image in each brow ...