Show the value in Angular in a dynamic way

My template needs to display the text 'Passed' only if item.name === 'michael' is not true. The component receives data in an array called courses[] from its parent. There are two interfaces, Courses and Teachers, where each course ID has associated Teacher data.

Below is the code snippet:

@Input() courses[];
isRequired = false;

ngOnInit() {
    for (const entry of this.courses) {
        this.checkData(entry);
    }
}

checkData(singleCourse: Courses): void {
    this.someService.getTeacherDetails(singleCourse.id).subscribe(item => {
        if (item.name !== 'michael') {
            this.isRequired = true;
        }
    });
}

interface Courses {
    id: number;
    name: string;
    price: number;
}

interface Teachers {
    name: string;
    address: string;
}

The variable isRequired, which sets true or false, does not seem to be functioning correctly. The method getTeacherDetails() returns an Observable<Teacher> using an HTTP request.

<tbody>
    <tr *ngFor="let course of courses">
      <td>
        {{course.name}}
      </td>
      <td>
        <span *ngIf="!isRequired"> Passed </span> // This part is not working as expected
      </td>
    </tr>
</tbody>

Answer №1

Whenever a component has a name, the isRequired flag will be automatically assigned to it.

To check if the name 'michael' is present in the listing, you can use listing.name === 'michael'.

<tbody>
    <tr *ngFor="let listing of courses">
      <td>
        {{listing.name}}
      </td>
      <td>
        <span *ngIf="listing.name === 'michael'"> Passed </span> // This part is currently not functioning properly
      </td>
</tbody>

Answer №2

Ensure that your isRequired is an observable as well.

You can set it up like this:

public isRequired$ = new ReplaySubject<boolean>(1);

In your method, do the following:

onCheckData(singleList: Courses) {
this.someService.someObservable(singleList.id).subscribe( item => {
    if (item.name === 'michael') {
        this.isRequired$.next(true);
     }
    });
   }

Remember to use the dollar sign convention for observables.

<span *ngIf="!isRequired$"> Passed </span>

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

A novel RxJS5 operator, resembling `.combineLatest`, yet triggers whenever an individual observable emits

I am searching for a solution to merge multiple Observables into a flattened tuple containing scalar values. This functionality is similar to .combineLatest(), but with the added feature that it should emit a new value tuple even if one of the source obser ...

Exploring Nested <Routes> with ReactJs

My decision on whether to display the Foo page or the Bar page is based on the route. However, the Foo page itself contains two sub-routes for components to render depending on the URL path - such as FooOne or FooTwo. This results in having two layers of r ...

What is the best way to store a logged-in user's email in a React

I have implemented a login API and I would like to save the email of the logged-in user in the state or another variable, so that I can display it in the header after successful login. The user's email is located in the data.email. The following code ...

Using Angular: Embed environment.ts into index.html as a standalone JavaScript file

Angular is typically set up to include the environment.ts file into main.js during the build process. This means that when dealing with multiple environments and corresponding environment.ts files, we must choose the environment at build time. My goal is ...

How to eliminate a particular validator from a form group in Angular

My goal is to eliminate the specific validator from the validator array so that I can reconfigure the controls when certain values have changed. I am aware of the traditional solution where I would need to repeatedly set validators. checked(event: MatC ...

How can I show distinct values in the Angular Material dropdown menu?

I am currently working on implementing a feature where I need to show unique options for the select using angular material. Below is what I have so far: where appitem is an array of items. <mat-form-field> <mat-select placeholder="Select app ...

Formatting numbers in Angular 4 to display in Million or Thousand format

I need assistance with formatting numbers in my Angular 4 application. I want to display the number in a certain format based on its value. For example, if the number is 12.23 million, it should be displayed as 12.2M (with one decimal place). If the numbe ...

Can we find a superior method?

I am currently using a stateful service to retrieve questions. The service checks if the questions exist in the local state, and if they do, it returns them. Otherwise, it makes an HTTP call to the API to fetch and store the questions. The service method ...

I am curious about the types of props for the methods within the 'components' object in react-markdown

Having some trouble using 'react-markdown' in NextJs 13 with typescript. TypeScript is showing errors related to the props of the 'code' method, and after searching online, I found a solution that involves importing 'CodeProps&apos ...

Activate backdrop feature in offcanvas mode

When trying to open an offcanvas panel, I discovered that adding show to its class is necessary for it to open. However, this caused the backdrop feature to stop working, and now clicking outside the panel does not close it. Is there a way to achieve the p ...

Issue with updating initial state that is null in Redux Toolkit

Encountered an issue while using Redux Toolkit with Redux Persist. Unable to update the initial state of a user if it's null. The code consistently assigns null to the store regardless of passing parameters. import { createSlice, PayloadAction } from ...

Angular2's use of promises, observables, and Dependency Injection (

If I have a service that looks like this import {Injectable} from 'angular2/core'; @Injectable() export class MyService { search(oSrchParams){ let promise = () => new Promise((resolve, reject) => Meteor.call('mockSe ...

Acquire information using AngularJS

This is a demonstration of my service: import { Injectable } from '@angular/core'; import { GenericPostService } from 'app/shared/services/generic.post.service'; @Injectable({ providedIn: 'root' }) export class FaqServic ...

Problem encountered while implementing callbacks in redux-saga

I am facing a scenario in which I have a function called onGetCameras that takes a callback function named getCamerasSuccess. The idea is to invoke the external function onGetCameras, which makes an AJAX call and then calls getCamerasSuccess upon completio ...

Using Unique Typeface in Ionic Framework 3

I am currently utilizing Ionic3: Your device information: Cordova CLI: 6.4.0 Ionic Framework Version: 3.0.1 Ionic CLI Version: 2.1.18 Ionic App Lib Version: 2.1.9 Ionic App Scripts Version: 1.3.0 ios-deploy version: Not installed ios-sim version: Not in ...

Encountering 'null' error in template with Angular 4.1.0 and strictNullChecks mode

After updating Angular to version 4.1.0 and activating "strictNullChecks" in my project, I am encountering numerous errors in the templates (.html) that look like this: An object may be 'null' All these errors are pointing to .html templat ...

Tips for adjusting the maximum characters per line in tinyMCE 5.0.11

I have an angular 8 application that utilizes tinyMCE, and I am looking to limit the maximum number of characters per line in the textArea of tinyMCE. My search for a solution has been unsuccessful so far despite extensive googling efforts. (image link: [ ...

Retrieve the values of private variables within a defined function

While experimenting with typescript, I have encountered an issue that I can't seem to resolve. My project involves using Angular, so I will present my problem within that context. Here is a snippet of my code: class PersonCtrl{ private $scope: I ...

Error: Module not found - Unable to locate 'dropzone'

Since migrating from Angular 4.4 to Angular 8.0, I encountered the following issue: ERROR in ./src/attributes/import/import.component.ts Module not found: Error: Can't resolve 'dropzone' in 'C:....\src\attributes\imp ...