Some Angular applications successfully utilize @HostBinding within Angular Components to dynamically determine CSS variable values, while others do not experience the same functionality

In Angular version 11.2.13, I developed a small UI components library that includes a Dial Gage Component utilizing @HostBinding to assign values to CSS variables in the component's CSS file. Below is the typescript file for my Dial Gage component:

import { Component, HostBinding, Input, OnInit } from '@angular/core';

@Component({
  selector: 'sio-dial-gage',
  templateUrl: './dial-gage.component.html',
  styleUrls: ['./dial-gage.component.scss']
})
export class DialGageComponent implements OnInit {
  @Input() value: number;

  @Input() unit: string;

  @HostBinding('style.--endvalue')
  endvalue: string;

  @HostBinding('style.--rotation')
  rotation: string;

  @HostBinding('style.--percentcolor')
  percentcolor: string;

  @HostBinding('style.--backgroundcolor')
  backgroundcolor: string;

  @HostBinding('style.--inner-percentcolor')
  innerPercentcolor: string;

  minValue = 0;

  maxValue = 100;

  warningMinValue = 25;

  alarmMinValue = 10;

  ngOnInit() {
    this.endvalue = this.setPressureGage();
    this.rotation = this.getBorderPercentage();
    this.percentcolor = this.getBorderColour('outer');
    this.innerPercentcolor = this.getBorderColour();
  }

  setPressureGage(): string {
    const pointerAngle = 90 + (this.value * 1.8);
    return `${pointerAngle}deg`;
  }

  getBorderPercentage(): string {
    const borderPercent = this.value * 1.8;
    return `${borderPercent}deg`;
  }

  getBorderColour(border?: string): string {
    // code here
  }
}

The Dial Gage component functions correctly in my component library playground and new Angular projects where it is used. However, when added to an existing Angular project with version 9.1.2, the host binding does not work as expected. Despite trying multiple solutions like changing AOT settings and ViewEncapsulation, the issue persists.

This is how the Dial Gage component renders in legacy Angular 9.1.2 applications:

<sio-dial-gage _ngcontent-rdm-c43="" ng-reflect-value="50" ng-reflect-unit="Psi" class="ng-star-inserted"></sio-dial-gage>

While in newer Angular versions (from 9.1.2 onwards), it renders as:

<sio-dial-gage _ngcontent-yin-c61="" value="10" unit="Psi" _nghost-yin-c62="" ng-reflect-value="10" ng-reflect-unit="Psi" style="--endvalue:108deg; --rotation:18deg; --percentcolor:#E42326; --inner-percentcolor:#F9C8C9;"></sio-dial-gage>

I am seeking advice on why the HostBinding feature is not working properly in the legacy application. Any suggestions or insights into the possible causes of this issue would be greatly appreciated. Thank you for your help.

Answer №1

I found a solution for the issue in the outdated software by configuring the tsconfig file to have

"target": "es2015"
and "enableIvy": true

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

The variable "Headers" has not been defined when executing Angular Universal

ERROR ReferenceError: Headers is not defined After executing the command node dist/server.js, a message appears stating "Node server listening on http://localhost:4000". However, when attempting to load http://localhost:4000 in the browser, an error occur ...

Tips for updating an Angular Material Table by incorporating new data through an Angular dialog box

After adding an item, I want to update the items in my mat table. However, I am facing difficulties updating the table with the dialog module. I have attempted to call my list component in my products component using ViewChild, but the table does not updat ...

Issues with data texture rendering in the most recent iteration of the third version

I am experiencing a problem with my code that renders a static bitmap in three js. It was working fine in version 110 but stopped working in version 147. I have checked the changelog to see if there were any differences but couldn't find anything. Th ...

What are the benefits of pairing Observables with async/await for asynchronous operations?

Utilizing Angular 2 common HTTP that returns an Observable presents a challenge with nested Observable calls causing code complexity: this.serviceA.get().subscribe((res1: any) => { this.serviceB.get(res1).subscribe((res2: any) => { this.se ...

Prevent MatAutoComplete from opening in mat-chip-list when entering an unrecognized text in the input field and pressing the "ENTER" key

Utilizing mat-chip-list in conjunction with mat-autocomplete based on the official Angular Material example found at https://material.angular.io/components/chips/overview#chip-input and https://stackblitz.com/angular/arjnbxmepgn?file=src%2Fapp%2Fchips-auto ...

Protractor test stalls when waiting for disabled element using async/await instead of control flow

We are currently designing end-to-end tests using async/await methodology for easier debugging. However, we have encountered an issue with a particular test that hangs on the line: await browser.wait(ExpectedConditions.presenceOf(appPage.getLogo())); I a ...

Tally the number of clicks for every item in the Typescript list

There is a button associated with each element in the list. <li *ngFor= "let item of itemList"> <button [ngClass]="'button'" (click)="increaseCounter(item)">Click me</button>; {{item.count}} </li> The goal is t ...

Strange error message regarding ES6 promises that is difficult to interpret

Snippet getToken(authCode: string): Promise<Token> { return fetch(tokenUrl, { method: "POST" }).then(res => res.json()).then(json => { if (json["error"]) { return Promise.reject(json); } return new Token ...

What is the best way to handle errors resulting from the "listen" method in Oak?

I am facing a challenge in Oak where I want to capture errors for logging purposes. Is there a way to catch an error when using the "app.listen" method? I've tried implementing try/catch, but it appears that Deno intercepts the error before my code ca ...

Specialized Character Formats in TypeScript

In my quest to enhance the clarity in distinguishing different types of strings within my program - such as absolute paths and relative paths, I am seeking a solution that ensures functions can only take or return specific types without errors. Consider t ...

Create a set of versatile input elements with Angular 9 and Material design for reus

Currently, I am in the process of developing a reusable input component using Angular 9 along with material design. My goal is to create something similar to the illustration provided below. https://i.sstatic.net/N1tCy.png TextboxComponent.html <mat- ...

Leverage the power of a useRef hook to dynamically update a state value within React, even when the object is potentially

I'm running into an issue with the useRef hook, as I'm receiving the error "object is possibly null" when attempting to use it to set a stateful object. const jselectRef = useRef<HTMLButtonElement>(null) const [defaultHeight, setDefaultHeig ...

Unlocking the potential of the search pipe in Angular 6

Check out this Data Model: items1 = [ title: 'Abc', items_containers : [ title: 'edf', items_containers: [ title: 'pqr', items_container: [ ............ ...

What is the method for retrieving array values from an attribute?

I am currently developing an Angular 6 application and I need to pass and retrieve array values dynamically through attributes. Here is the code snippet I have used for this purpose: HTML: <ul class="list-unstyled" id="list" [attr.parent_id]="123"> ...

API Routes - xxxx has been resolved by the API without sending back a response, potentially causing delays in request processing

While working on my frontend project, I encountered an issue with a simple API call using API routes. Whenever I try to complete the call, I face the following error which prevents my redirect from working: API resolved without sending a response for /ap ...

Unable to establish a cookie on an Angular client hosted anywhere except for 'localhost'

Utilizing an express server as the backend and an angular client as the frontend. The express server is hosted on port 3001, while angular is on port 4200. Everything works perfectly when running on localhost. However, when attempting to host Angular on a ...

Issue with using react-signature-canvas inside an ant design Modal

Encountering a problem with SignaturePad within the ant design component library's "Modal" component, as it fails to generate the base64 image. Upon reaching the trim function in the signaturepad code, the output is "data:". However, without the Moda ...

When using PrimeNG Autocomplete, no suggestions are displayed when typing a search query

Having trouble with the PrimeNG autocomplete component? I'm facing an issue where the completeMethod is triggered when typing in the input field, but the results are not displayed properly. Despite testing the completeMethod and confirming it filters ...

Encountering difficulties in creating a new package for angular

I am having trouble generating a new package for Angular. Here are the software version details: npm -v v6.1.0 node -v v8.11.3 ng -v v6.0.8 When attempting to create the project with ng new project_name, I encounter the following error: https:// ...