How to assign a value to a variable in Angular after the view

Situation: In the current setup of my website, I have implemented two tabs each containing text with a fixed height and enabled content-overflow hidden.

Problem: My goal is to add three dots (icon) when the text overflows. To achieve this, I utilize an expression that effectively recognizes overflow and triggers the addition of an expandButton.

isOverflown: boolean;
hasExpandButton: boolean;

[...]

const overflown = this.isOverflown() ? this.hasExpandButton = true : this.hasExpandButton = false;

The challenge lies in determining where to check for overflowing content since I use this logic in two different sections (specifically in tabs).

When I opt for ngAfterViewInit, the const overflown accurately detects overflow on the first tab but fails to do so on the second tab due to rendering limitations. This occurs because the view is only refreshed on the initial tab switch, neglecting subsequent tabs.

I explored alternative lifecycle-hooks without success in resolving the issue.

Considering ngAfterContentInit along with implementing a variable to track its initial invocation did not yield the desired outcome either.

I appreciate any guidance on this matter.

SOLUTION: Resolving the problem involved disabling lazy loading for the component, ultimately fixing the issue.

Answer №1

To verify, simply wait until the ngAfterViewInit lifecycle hook is triggered and when the user switches tabs. Include (click)="validateOverflow()" in the tab tag within your HTML code.

Answer №2

Give CSS a shot with this challenge:

.txt-overflow {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

This can help simplify your component without extra logic.

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

Retrieve the variable only once a response has been received from the POST request

Is there a way to update a variable in my component only after receiving a response from a POST request? Here is the code in component.ts: formSubmit() { this.sent = this.submitProvider.sendByPost(this.form); this.formSent = this.submitProvider.f ...

Dynamically adjusting CSS Max-Height according to scroll position

Is there a CSS only technique to achieve the following scenario: An element is positioned absolutely at x,y coordinates on the screen. The document includes a vertical scrollbar depending on its content. Can the height of the element be adjusted based on ...

When I click on the button, the page reloads instead of executing the essential code

On my website, there is a settings page where users can edit their profiles. After editing, they click the update button which triggers the updateProfileData function. This works smoothly in Chrome browser, but in Firefox, the page reloads as soon as the b ...

What is the process of retrieving a string in a method within an Ionic TypeScript file and storing it in a variable?

Having trouble returning a string to another method in an Ionic TypeScript file. Our goal is to return JSON.stringify(res) and assign it to the stringset variable. We initially tried testing by simply returning the string "hi", but even that did not work. ...

How can I implement a master-detail view in Angular 2?

Update: I finally figured it out. After moving all the html from index.html to app.component.ts, everything started working perfectly. The method described below is the correct one. I've been facing an issue where I have a ParentComponent that retrie ...

Angular 2 components sharing a common route

Two modules share the same path but have different templates (main and auth). The modules should be loaded based on whether the user is authenticated or not. Unfortunately, this functionality is not currently working as intended. Below are the relevant co ...

Passing the select id between two functions in Angular: A step-by-step guide

I am facing a challenge with a select element within a form in my HTML. I need to retrieve the ID of the selected value. <mat-form-field> <mat-label>Select an user</mat-label> <mat-select disableRipple #selectVal i ...

Position Bootstrap 5 modal footer elements to the left and right for an enhanced visual

Can anyone assist me with customizing the Bootstrap 5 modal footer? I am looking to align a text (either using span or label) to the left and a button to the right within the modal footer. I came across this solution and attempted to implement it by cha ...

How to center a progress bar in a div using Bootstrap

Looking to incorporate a Bootstrap progress bar as a loading indicator, aiming for the progress bar to be centered within the DIV at a specific width. However, aligning the progress bar in the center seems to be an issue. When utilizing: <div class="t ...

This JavaScript code is configured to only display content on half of the page

Has anyone experienced issues printing a full element (body) using the jquery plugin https://github.com/jasonday/printThis? I have only been able to print half of the element vertically. I also tried using raw javascript with window.print(); but encountere ...

Complications with Laravel URLs

In the Blade file source code, the CSS include URL is structured like this: <link rel="stylesheet" href="{{ asset('frontend/css/bootstrap.min.css') }}" type="text/css"> However, when I check the page source in the browser, I see the follo ...

The JavaScript and CSS properties are not functioning properly with the HTML text field

I came across this CodePen example by dsholmes and made some modifications: Here Furthermore, I have developed my own form on another CodePen pen: Link The issue I'm facing is related to the placeholders/labels not disappearing when typing in text f ...

I was unable to showcase various maps

import TileLayer from 'ol/layer/Tile'; import OSM from 'ol/source/OSM'; layers: any; base_maps:any; map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM({ maxZoom: 23, }), ...

Excel add-in in Angular featuring exclusive custom functions

I've recently embarked on the journey of developing Office Add-ins, using public code samples to simplify the process. Currently, I am exclusively testing with the web version of Excel. My goal is to merge the functionality of two sample add-ins: one ...

Error in WordPress: The table prefix cannot be blank

While setting up WordPress, I encountered an issue during the database details input in the first step of the installation process. I am using XAMPP and have tables in my database named wordpress_tbl_table1 and wordpress_tbl_table2. I entered "wordpress_tb ...

Implementing a custom overwrite function in TypeScript's inheritance

Below is a class that I have: export class RestService { private baseUrl: string; constructor(protected http: HttpClient) { this.baseUrl = environment.LOCAL_URL; } public get<T>(resource: string, params?: HttpParams): Observable< ...

Is there a method to apply the background-color of the parent element based on the child class in CSS?

Is there a method to change the parent's background color based on the child's class using CSS? Here is the sample code snippet: Sample Code If the child has a class of red, the parent should have a red background. If the child has a class of ye ...

Impact of using ngIf in ngAfterViewInit

During the ngAfterViewInit lifecycle hook, I am triggering an event and capturing it in another component using ComponentRef. Everything functions smoothly until I incorporate ngIf in the parent component. What impact does ngIf have on Angular's life ...

Change a variable on the fly without needing to reload the page

<!DOCTYPE html> <!-- To update your slot machine images without refreshing the page, you can use JavaScript to dynamically change the images on click. Here's a simple example using JavaScript: <html> <head> <title& ...

How can I bind an event for changing innerHTML in Angular 2?

Is there a way to implement something similar to this: <div [innerHTML]="content" (innerHTMLchange)="contentInit()"></div> Currently, I have a variable content that is updated by a service fetching a string from my express server. The content ...