Issue with Angular 8: discrepancy between the value utilized in component.html and the value stored in component.ts (Azure application service)

Encountering a peculiar behavior in one of my Angular applications. In the component.html file, I aim to display "UAT" and style the Angular mat elements with a vibrant orange color when in UAT mode, while displaying them in blue without any mention of UAT (aka PROD) otherwise.

Everything seems fine locally, but once deployed to Azure, things get confusing. Here's an excerpt from my code:

Within my Angular solution, there is an env.json file structured as follows:

{
"production": false,
"isTestEnvironment": false,
}

In VSTS, after building the app once, it is utilized for all deployments. For UAT deployment, "isTestingEnvironment" is set to "true" before shipping it off to Azure. Similarly, for production deployment, "production" is set to "true" and "isTestingEnvironment" to "false" before shipment.

To extract this env.json, something like this is used in app.module.ts:

const appInitializer = {
 provide: APP_INITIALIZER,
 useFactory: configFactory,
 deps: [AppConfig],
 multi: true
};

The appInitializer is then registered in the providers section, and the AppConfig injectable looks like:

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

import {HttpClient} from '@angular/common/http';

@Injectable()
export class AppConfig {
 public config: any;

 constructor(private http: HttpClient) {}

 public load() {
  return new Promise((resolve, reject) => {
   this.http.get('env.json').subscribe((p: string) => {
    this.config = p;
    resolve(true);
   });
  });
 }
}

In the component's constructor, the isTestingEnvironment is retrieved first:

this.isTestEnvironment = appConfig.config.isTestEnvironment;

Upon console.log, the values appear correct - true for UAT, false for PROD, and matching the value in the local environment even during hot changes while ng serve is running.

Despite the well-formed env.json file being deployed (as confirmed using kudu/powershell), the following line in component.html causes issues:

<h5 style="margin: auto;" *ngIf="isTestEnvironment">UAT</h5>

Regardless of whether isTestingEnvironment is true or false moments earlier according to the console log, "UAT" always appears in UAT and PROD modes. Strangely, this works perfectly during local debugging sessions using Visual Studio Code (1.42.1) and Node.js (v10.18.1).

At this juncture, Angular seems to equate true with false, leaving me utterly perplexed.

I've verified multiple times that the variable isTestingEnvironment is only set in the env.json file within the Angular solution. It seems like I'm either overlooking something glaringly obvious or there's a serious flaw in my code.

Any assistance on this matter would be immensely appreciated.

Thank you.

Answer №1

After some investigation, I finally uncovered the root cause of the issue.

In summary: The problem stemmed from a VSTS release task known as "Azure App Service Deploy" which converted variables to strings in the "File Transform & Variable Substitution Option" section.

Upon closer inspection of the deployed file, I noticed that a variable that was originally:

{
"isTestEnvironment": true,
}

had been changed to:

{
"isTestEnvironment": "true",
}

when it reached the azure server. This discrepancy led to erroneous checks against the supposed boolean variable, as a non-empty string is considered true in JavaScript.

As a solution, I now use JSON.parse(myVariable) when extracting the variable from the env.json file. I tested this locally and it resolved the issue.

For those utilizing the same release task, be cautious of the conversion of booleans to strings. Our release pipeline variables were not declared with quotation marks around them, simply containing true/false values.

I appreciate the assistance provided in suggesting potential workarounds. Thank you all.

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

Issue with Angular 6: Animation with LESS is failing to work post deployment

Hello everyone, I'm facing an issue with my LESS animation. When I deploy my app on the server after using ng build --prod, the CSS animations are not visible in browsers. They work perfectly fine on localhost but fail to work after deployment and I c ...

Prevent selection of future dates and display them in a muted grey color in the p-calendar component

I am attempting to prevent users from selecting future dates and visually distinguish them by setting a grey color background. However, I am having trouble disabling the future dates while the grey color background is functioning correctly. Any ideas on ho ...

Unable to render ASP.NET Core 2 WebAPI and Angular application on the webpage

Following a tutorial, I created an ASP.NET Core 2 Web API. The API functions properly when accessed through the browser at: https://localhost;44313/api/liveconsole To design the Angular UI for my web API, I referred to another tutorial. After multiple at ...

Typescript on the client-side: what is the best way to eliminate circular dependencies when using the factory method design pattern?

In my code, I have implemented the factory method pattern. However, some instances using this pattern end up with circular dependencies. Removing these dependencies has proven to be a challenge for me. To illustrate, consider the following example: // fact ...

"Discovering the root cause of Angular memory leaks and resolving them effectively is crucial for

I am facing an issue with a code that appears to be leaking, and I am seeking advice on how to identify the cause or properly unsubscribe. Even after adding an array containing all object subscriptions, the memory leakage persists. import { Component, On ...

Transforming FormData string key names into a Json Object that is easily accessible

Recently, I encountered an issue where my frontend (JS) was sending a request to my backend (Node Typescript) using FormData, which included images. Here is an example of how the data was being sent: https://i.stack.imgur.com/5uARo.png Upon logging the r ...

A step-by-step guide on dynamically binding an array to a column in an ag

I am currently working with the ag-grid component and I need to bind a single column in a vertical format. Let's say I have an array ["0.1", "0.4", "cn", "abc"] that I want to display in the ag-grid component as shown below, without using any rowData. ...

Updating Angular 13 with charts.js and ng2-charts may expose unfixed vulnerabilities

I am facing an issue while updating chart.js and ng2-charts in Angular 13. After running npm i [email protected] and npm i [email protected], I encountered vulnerabilities that couldn't be resolved with npm audit fix. Do I need to update any ...

Having trouble with Angular material's MdMenu component not functioning as expected?

I recently came across this code snippet from Angular material documentation: <button md-button [mdMenuTriggerFor]="menu">Menu</button> <md-menu #menu="mdMenu"> <button md-menu-item>Item 1</button> <button md-m ...

Inadequate data being sent to the server from Angular2 post request

Currently, I have a form field whose value I am passing to a service as this.form.value. However, when I log this.form.value on the console, I see Object { email: "zxzx", password: "zxzxx" }. Despite this, when I send the same data to the service and make ...

Leverage Angular to implement Bootstrap 5 tooltip on elements created dynamically

Our Angular-14 application uses Bootstrap-5 and I am currently attempting to add a tooltip to an element that is dynamically created after the page loads. The new element is based on an existing element, shown below: <span [ngbTooltip]="tipSEConte ...

Have you considered using Compodoc for documenting Storybook components?

While setting up an Angular Storybook project, we are prompted to integrate Compodoc for documentation purposes. I am curious to know if Compodoc offers additional features to Storybook or is it simply a different method of presenting documentation? ...

Check if a value is present in the array with *ngIf

I'm curious about how to use the ngIf directive in a specific scenario. In my Angular application, I have dynamically generated angular material toggles, each with a unique id. I'm familiar with using ngIf to conditionally display elements on the ...

Encountering an issue while attempting to install Font Awesome free

I encountered the following error: npm ERR! code E401 npm ERR! Unable to authenticate, need: Basic realm="https://npm.fontawesome.com/",service="npm.fontawesome.com" npm ERR! A complete log of this run can be found in: npm ERR! C:& ...

Expanding constructor in TypeScript

Can the process described in this answer be achieved using Typescript? Subclassing a Java Builder class This is the base class I have implemented so far: export class ProfileBuilder { name: string; withName(value: string): ProfileBuilder { ...

Suggestions for importing by Typescript/VS Code

Imagine you have a file called a.ts that contains 4 named imports: export const one = 1 export const two = 2 export const three = 3 export const four = 4 Next, you have a file named b.ts and you want to import some variables from a.ts. import {} from &a ...

The CSS styles are functioning correctly in index.html, but they are not applying properly in the component.html

When the UI Element is clicked, it should add the class "open" to the list item (li), causing it to open in a collapsed state. However, this functionality does not seem to be working in the xxx.component.html file. Screenshot [] ...

Employing distinct techniques for a union-typed variable in TypeScript

I'm currently in the process of converting a JavaScript library to TypeScript. One issue I've encountered is with a variable that can be either a boolean or an array. This variable cannot be separated into two different variables because it&apos ...

Angular 4: The Authguard (canActivate) is failing to effectively block the URL, causing the app to break and preventing access to all resources. Surprisingly, no errors are being

I have created an authguard, but it seems to not be blocking the route when the user is logged out. The authentication workflow in the app works fine, as I am using traditional sessions on the backend database. I have also verified the state of the auth se ...

Learn how to effectively switch tabs and pass data within an Angular Bootstrap application by utilizing the

Recently diving into Angular, I've come across what seems to be a simple problem: transferring data between tabs in an angular bootstrap project. Currently, my main component contains a tab structure (I'll simplify for clarity): <div> ...