finding corresponding key in ngFor

I am curious to see if it is possible to match a key in *ngfor. Here is an example of my code on StackBlitz. However, when I try this method, it only displays No data.

HTML

<ul>
  <li *ngFor="let course of courses; index as i">
     {{key == course.id? course.name :'No data' }}
  </li>
</ul>

Component

  courses = [
    { id:1, name:'course1' },
    { id:2, name:'course2' },
    { id:3, name:'course3' }
  ];
  key = [1,2];
}

Answer №1

key is a collection that you should verify the existence of course ids within.

<ul>
  <li *ngFor="let course of courses; index as i">
     {{ key.includes(course.id) ? course.name :'No data' }}
  </li>
</ul>

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

Unable to load custom package in Angular 2 testing environment

I've been following the official Angular 2 testing guide on an existing project. Everything runs smoothly when I use my custom library, downloadjs, in the application. However, I encounter an error in the console during test execution: "__zone_symbol ...

Guide on Applying a Dynamic Color in VueJs 3 Composition API/Vuetify Using CSS

Currently, my project utilizes Vue 3 with the composition API and Vuetify for the UI. I am looking to utilize a color that is already defined in a Vuetify theme variable within my CSS, similar to how I have done it previously in JavaScript. Although I at ...

Tips for showcasing array values on an HTML webpage

Having trouble displaying the "latitude" and "longitude" in html. I attempted to use a forEach loop, but it was not successful https://i.sstatic.net/FnLMP.png this.otherService.getList().subscribe( (response: any) => { this.atm = response; H ...

Playing noughts and crosses with the minimax strategy

I am struggling with understanding the minimax algorithm and have been working on it for 2 days without success. Can anyone take a look at my code and help me identify any errors? export default class TicTacToeController { /*@ngInject*/ constructor($s ...

An approach to mocking the 'global' property in Styled JSX when testing with Jest

Encountering an error while trying to run tests on a NextJS project. The error message reads: TypeError: _css.default.global is not a function To mock StyledJSX, I added a __mocks__/styled-jsx/css.js file with the following content: const css = () => { ...

Issues with functionality of React/NextJS audio player buttons arise following implementation of a state

I am currently customizing an Audio Player component in a NextJs application using the ReactAudioPlayer package. However, the standard Import Next/Audio and using just <Audio> without props did not yield the expected results. The player functions as ...

This context does not contain the expected object "this"

I seem to be losing the reference to the "This" object within the helper class in this particular structure, and I'm not sure why. When looking at the getAll method, the this object is currently pointing to the object housed within the servicesDict a ...

Error encountered with next-auth and the getServerSession function

Whenever I try to use the getServerSesssion function with all the necessary parameters, it results in a type error. In my code, I have the getServerAuthSession function defined as shown below: import { authOptions } from '@/pages/api/auth/[...nextauth ...

Headers are not being sent by Angular 5 + HttpClient when making a GET request

Currently, I am utilizing Angular 5 to perform an HTTP GET request using HttpClient.get() method, as detailed in the Angular official documentation[1]. This request requires an Authorization header to be included. I am encountering an issue with the code ...

What is the proper way to utilize the ES6 import feature when using a symbolic path to reference the source file?

I am seeking a deeper understanding of the ES6 import function and could use some assistance. The Situation Imagine that I have a portion of code in my application that is frequently used, so I organize all of it into a folder for convenience. Now, in t ...

How can I automatically refresh the HTTP service in Angular 6 after encountering an error?

My service is set up to make an http get request that responds with a list of products: import 'rxjs/Rx'; import {Injectable} from '@angular/core'; import {Http} from '@angular/http'; @Injectable() export class ProductServic ...

Diving into Angular Typescript: Understanding the [object Object] in HTML

One of my todos requires the following input: todo.ts: import { TodoTag } from "./todo-tag-custom"; export class Todo { ... tags: TodoTag[]; } todo.html <tr *ngFor="let todo of todosFiltered()"> <td>{{todo.tags | json ...

Struggling to capture the changing variables in Angular 8

Hey there, I've been working on a basic Angular app to sharpen my skills, but I'm facing an issue with detecting changes in a service variable. To explain further, I have a component that's subscribed to a variable from a service. However, ...

The 'ngIf' property cannot be bound to 'div' since it is not recognized as a known property in the production build

When running locally, everything works fine. However, when moving to the production build, errors start popping up. The code snippet in question is: import { CommonModule } from '@angular/common'; imports: [ CommonModule ] The complete ...

The bidirectional bindings within the component are malfunctioning

I just started learning Angular and I'm currently working on a small project. After following tutorials on two-way bindings, I attempted to implement it in my project. However, when I try to set values in the HTML for my component, it doesn't see ...

Ways to return bsDateRangePicker to its default value

I'm currently working on creating reactive forms using Angular 9 and integrating ngx-bootstrap. One issue I am facing is with the daterangepicker functionality. Whenever I utilize the form.reset() function, it clears the input field entirely instead o ...

Icon positioned to the left within the text box

Hey there! I'm new to NativeScript and I've been struggling to place an icon inside a textbox. Can someone please help me out? Expected Output: https://i.stack.imgur.com/xvoZG.png Code <GridLayout columns="*, *" rows=& ...

What is the best way to include type within a nested object?

How should I properly define types for a nested object structured like the example below? const theme: DefaultTheme = { color: { primary: '#5039E7', secondary: '#372E4B', heading: '#4D5062', }, ...

Using JSON to Map Routes in Angular 2

Service: export class ArticlesService { private _url = 'https://example.firebaseio.com/.json'; constructor(private _http: Http) { } getAllArticles(): Observable<any> { return this._http.get(this._url) .map((response: Re ...

Typescript is struggling to locate a module that was specified in the "paths" configuration

Within my React project, I have set up a module alias in the webpack config. Now, I am looking to transition to Typescript. // I have tried to simplify the setup as much as possible Here is my tsconfig.json located in the root directory: { "compilerOp ...