Angular component.html does not compile due to a check that includes inline array creation

There is an enum called Status:

export enum Status {
    SOME_VAL = "SOME_VAL",
    SOME_VAL_2 = "SOME_VAL_2",
    SOME_VAL_3 = "SOME_VAL_3";
}

Also, I have an interface named SomeInterface:

export SomeInterface {
    status?: Status;
}

In a simple usage of the includes method with inline array creation in some.component.ts:

let someVal: boolean = [Status.SOME_VAL, Status.SOME_VAL_2].includes(this.someObject.status);

Everything works fine here. But when used within the some.component.html:

<div *ngIf="[Status.SOME_VAL, Status.SOME_VAL_2].includes(someObject.status)">
    ...
</div>

A warning pops up saying:

Argument type Status is not assignable to parameter type Status.SOME_VAL | Status.SOME_VAL_2

This prevents compilation. When converted to:

<div *ngIf="$any([Status.SOME_VAL, Status.SOME_VAL_2]).includes(someObject.status)">
    ...
</div>

The warning disappears, but unfortunately, it always evaluates to false without any error or warning (possibly causing invisible errors). How can this issue be resolved?

An Additional Update:

I have:

Status = Status;

in my some.component.ts, allowing me to reference it from the html.

Answer №1

I have developed a custom AsPipe:

@Pipe({
  name: 'as'
})
export class AsPipe implements PipeTransform {

  transform<T>(value: any, args: T): T {
    return value as T;
  }
}

and implemented it in the template like this:

*ngIf="([Status.SOME_VAL, Status.SOME_VAL_2] | as: [Status]).includes(someObject.status)"

Answer №2

To achieve this, you must have access to the enum instance. This can be done by declaring a variable in the .ts file that references the enum and can then be used in the .html template. Here's an example of how to do it:

In your .ts file:

public currentStatus = Status;

In your .html file:

<div *ngIf="[currentStatus.SOME_VAL, currentStatus.SOME_VAL_2].includes(someObject.status)">
    ...
</div>

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

Angular 7 and MVC: A Perfect Pair

After working on developing an SSO App that utilizes C# and AngularJs, I am now embarking on adding a new project to my existing SSO app. For this new project, I intend to leverage Angular 7 instead. Could you please provide guidance on how to incorporat ...

What is the best way to style the header of a table when scrolling in CSS?

Currently, I am facing an issue with applying the top CSS property to the thead element of a table while scrolling. I have attempted various methods but have been unsuccessful in achieving the desired outcome. Initially, I used the scroll event, however, ...

Attempting to initiate an AJAX request to an API

Hey everyone, I've been working on making an AJAX API call to Giphy but I keep receiving 'undefined' as a response. Can anyone offer some advice on how to troubleshoot and fix this issue? Thanks in advance for your help! var topics = ["Drak ...

Adjusting the format of a JavaScript object

Looking at the object below: {A: 52, B: 33} I want to transform it into this format: ["A", 52], ["B", 33] Any recommendations on how to achieve this conversion? ...

What are the steps for designing personalized syncfusion grid-columns while still preserving the built-in search functionality of syncfusion?

Looking to transform the data coming from the backend, specifically mapping a user's status which is represented as a number to its corresponding string value. Considered using typescript for this mapping task, but it interferes with syncfusion' ...

Connecting AngularJS with select options

I am currently developing a checkout feature that calculates shipping prices based on the selected country. If the user picks United States, it should display US. For any other country selection, it should show Not US While the shipping function correctly ...

Identifying an Object by Clicking with the Mouse in three.js

After successfully loading 3 external models into my scene using the json loader, I am now trying to retrieve the name of the clicked model/object. Below is the code snippet that I used to load the models: var object_material = new THREE.MeshBasicMateria ...

What is the reason for Cloud Firestore's realtime database fetching all items?

Currently, I am developing a project with vuejs and I am facing an issue where I need to fetch only the last created item from Firestore's realtime database. However, when I execute the following code snippet, it retrieves all items from the database ...

Guide on integrating next-images with rewrite in next.config.js

I'm currently facing a dilemma with my next.config.js file. I've successfully added a proxy to my requests using rewrite, but now I want to incorporate next-images to load svg files as well. However, I'm unsure of how to combine both functio ...

Testing XMLHttpRequest with Jasmine: A Complete Guide

Is there a way to test the onreadystatechange function on XMLHttpRequest or pure JavaScript AJAX without using jQuery? I need to do this because I'm working on a Firefox extension. It seems like I may have to use spies, but I'm having trouble bec ...

Enhancements in Converting JSON Objects to HTML Lists Using jQuery

I have created a function that converts a multi-dimensional JSON object into an HTML list. You can check it out here: http://jsfiddle.net/KcvG6/ Why is the function rendering the lists twice? Update: http://jsfiddle.net/KcvG6/2/ Are there any impro ...

Top technique for verifying the presence of duplicates within an array of objects

How can I efficiently check for duplicates in typescript within a large array of objects and return true or false based on the results? let testArray: { id: number, name: string }[] = [ { "id": 0, "name": "name1" }, ...

Discover the magic of retrieving element background images on click using jQuery

I am attempting to extract the value for style, as well as the values inside this tag for background-image. Here is the script I have tried: function getImageUrl(id){ var imageUrl = jQuery("."+id+". cycle-slide").attr("src"); alert('' + ima ...

Developing a Prototype for an Angular Directive

After following instructions from a question on Stack Overflow, I have updated my application configuration with the code snippet below: $provide.decorator('formDirective', function($delegate) { var directive = $delegate[0]; directive.contro ...

Jquery-ajax fails to accomplish its task

I am just starting to learn about ajax and recently created a simple page on my local server to practice sending and receiving data from/to a JSON file in the same directory. Although the GET method is working perfectly fine, I am having trouble understan ...

using vuejs, learn how to retrieve properties within the .then function

This is the code I am working on: methods: { async pay() { this.$notify(`working`); if (!this.$v.$invalid) { try { var data = { to: this.to, subject: this.subject, }; let resp ...

Changing the Style of a CSS Module Using JavaScript

Embarking on a journey into Javascript and React, I am currently working on my first React app. My aim is to adjust the number of "gridTemplateRows" displayed on the screen using a variable that will be updated based on data. Right now, it's hardcode ...

Encountering a roadblock while trying to install a package using NPM, as the installation process becomes halted at version [email 

Having trouble installing @angular/cli via npm. It seems to get stuck every time while trying to download the package chokidar. https://example.com/image.png Here is some diagnostic information: npm version 5.0.0 node version 8.0.0 OS: Windows 7 ...

Guide to adding an external script file to an Angular component

Incorporating an external api into my angular project has presented some challenges. In a normal HTML/Javascript setup, all that is required is: <script src="http://api.eventful.com/js/api"></script> <script> EVDB.API.call("/events/get" ...

Using custom elements in both React and Angular is not supported

After successfully creating a custom element in React using reactToWebComponent, I integrated it into a basic HTML file like this: <body> <custom-tag></custom-tag> <script src="http://localhost:3000/static/js/bundle.js&quo ...