Using ng-if to evaluate multiple numerical values

I have multiple questionnaires, and I'm looking to use *ngIf to control which ones are displayed in a specific div.

Currently, I'm handling this by specifying each individual question number:

<div *ngIf = "questionNo!= '00' && questionNo!= '04' && questionNo != '21'">
...</div>

I believe there must be a more efficient way, such as using *ngIf= "questionNo != [00.01,02....]", but my attempts have been unsuccessful.

Your assistance on this matter would be greatly appreciated! Thank you!

Answer №1

To achieve the desired outcome, you can utilize either && or ||, or follow this approach:

*ngIf= "[00,01,02].includes(questionNo)"

*ngIf= "![00,01,02].includes(questionNo)"

Answer №2

My suggestion is to relocate that condition within the component code and encapsulate it in shouldShowSomething():bool. This approach offers improved clarity and maintainability, along with the ability to accept arguments for potential parameterization.

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

When employing node.js's filesystem module, you may encounter an empty object as the result

Currently, I am working on a project using Angular 6 and Electron 4. When attempting to utilize the fs module from Node, I am encountering an issue where it returns an empty object. Below is the snippet of my code: import { Component } from '@angular ...

Challenges Encountered when Making Multiple API Requests

I've encountered a puzzling issue with an ngrx effect I developed to fetch data from multiple API calls. Strangely, while some calls return data successfully, others are returning null for no apparent reason. Effect: @Effect() loadMoveList$: Obse ...

Exploring the capabilities of Angular 6 with web components

Encountering a particular issue, I have an Angular 6 application with a routing system in place. My goal is to create a web component that encapsulates this application for usage within another web application. Following a tutorial, I made modifications ba ...

The test() function in JavaScript alters the output value

I created a simple form validation, and I encountered an issue where the test() method returns true when called initially and false upon subsequent calls without changing the input value. This pattern repeats with alternating true and false results. The H ...

Guide to dynamically load external JavaScript script in Angular 2 during runtime

Currently, I am integrating Twitter digits for authentication purposes. To implement this feature, a small .js script needs to be downloaded and initialized. The recommended approach is to directly fetch the file from the Twitter server. In order to succe ...

Error with Array type encountered in Typescript's find method

I am encountering an issue with code that looks like this: type X = { test: number; x: number; }[]; type Y = { test: number; y: number; }[]; type Z = { test: number; z: number; }[]; export const testFunc = (arg: X | Y | Z) => { return a ...

Unable to access material datepicker by its element id within ngAfterViewInit function

Upon component loading, I am attempting to open the material DatePicker using its element ID (#). However, when I use ngAfterViewInit, it consistently returns an error stating 'Cannot use 'open' on undefined'. Interestingly, the DatePic ...

Is there a way to solely expand the type declarations from a separate class?

Imagine we have two distinct classes X and Y with 77 property-type declarations in common, but their methods, including constructors, are different: class X { public prop1: number; public prop2: string; //... public prop77: "hello"; constructor(){ th ...

Retrieving Identifiers with Typescript from an object

I'm a newcomer to Typescript and I'm looking to extract ids from an observable Here's the observable data: let myObj = [{ "id": 1, "text": "Mary" }, { "id": 2, "text": "Nancy" }, { "id": 3, "text": "Paul" }, { "id": 4, "tex ...

Encountering a new challenge in Angular: The error "InvalidPipeArgument: '' for pipe 'AsyncPipe'

Whenever I try to fetch data from the server, these errors keep popping up. This code was written by someone else and I would like to improve upon it. Could anyone suggest the best approach to handle this situation? Are there any coding patterns that sho ...

I'm looking to enhance my Angular app by implementing a submenu that features various tabs

Currently, I am following a tutorial available at this link to achieve my desired outcome. However, I am facing some challenges because my tabs are not located in the root component. In my scenario, I have a contract with certain properties that I want to ...

Insufficient attributes in TypeScript component for React application

Developing with React import {Input} from '@xxx/forms'; <Input label="account Name" name="account"/> Type Definition for input import React, { Ref } from 'react'; import { InputProps as UITKInputProps } from ...

Save information for each session with a set expiration time

Currently, I am working on a mobile application using Angular (specifically Ionic 5) and I am in need of a solution to maintain session data for my users throughout their workflow. Initially, I thought about utilizing the sessionStorage feature for this p ...

CORS PHP issue: Preflight request response fails access control check due to lack of HTTP OK status

I'm currently working on an application where I need to upload images. However, I am encountering an error when trying to send the image for upload: The preflight request response fails access control check due to not having HTTP ok status. Here is ...

Tips for cycling through a template for a component in Angular

My goal is to create a basic chess application using Angular. I have developed two components, Piece and Player, with each player having an array of 16 pieces (player1 and player2). The Piece component includes a template: <app-piece>. Now, I want t ...

New substitute for extending the extent in OpenLayers 4

I am currently in the process of migrating my code from OpenLayers 3 to OpenLayers 4 using TypeScript. Previously, I had a code snippet that extended the extent of my map so that all vector shapes I drew would be visible upon loading... angular.forEach(w ...

What are the steps to incorporating the pick function in TypeScript?

The TypeScript documentation mentions a pick function that is declared but not implemented. In an attempt to create a simplified version, I wrote the following: function pick<T, K extends keyof T>(obj: T, key: K): Pick<T, K> { return { [key]: ...

Using dictionary keys as valid property values

I have put together a dictionary like so: const iconTypesDictionary: { [key: string]: IconPrefix } = { solid: 'fas', regular: 'far', light: 'fal', } My goal is to be able to utilize the keys of this dictionary as potent ...

Angular and Node.js are powerful tools for creating HTTP clients

I have been facing an issue while trying to display event data from MongoDB in an Angular view. The data shows up fine in the browser console, but it does not appear on the website itself. I am using Node.js for the backend and Angular for the frontend wit ...

What is causing the issue in locating the assert package for VS Code and TypeScript?

My main issue lies with the pre-installed node libraries (path, fs, assert). Let me outline the process that leads to this problem: Begin by launching Visual Studio. Select File > Open Folder, then pick the root core-demo folder. In the file panel, loc ...