Using Angular's ngFor directive to iterate over a collection based on a true

I am currently attempting to resolve the following condition: if the condition is true, display a button, otherwise hide the button.

OfferMatching() { this.getmatchoffer.filter(obj => { debugger for (let i = 0; i < this.applicationJobList.length; i++){ var Options = { hour12: false }; const offerStartDate = new Date(this.applicationJobList[i].offerSteps.initial.jobDateoffer).toLocaleDateString(); const offerStartTime= new Date(this.applicationJobList[i].offerSteps.initial.startTime).toLocaleTimeString('it-IT',Options); const offerEndTime = new Date(this.applicationJobList[i].offerSteps.initial.endTime).toLocaleTimeString('it-IT',Options); const bookDateoffer = new Date (obj.offerSteps.initial.jobDateoffer).toLocaleDateString() ; const bookstartTime = new Date(obj.offerSteps.initial.startTime).toLocaleTimeString('it-IT',Options); const bookendTime = new Date(obj.offerSteps.initial.endTime).toLocaleTimeString('it-IT',Options); debugger if (bookDateoffer === offerStartDate ) {

      if (bookstartTime  < offerStartTime) {

        if (bookendTime < offerEndTime) {
       this.samemOffer = false;

       } else {
       this.samemOffer = true;
       }

      } else if (bookstartTime > offerEndTime) {

        if (bookendTime > offerEndTime) {
         this.samemOffer = false;

       } else {
        this.samemOffer = true;
      }

       } else {
       this.samemOffer = true;
        }
    }
  }

})

} this is a snippet of Angular TypeScript code

{{i+1}}
{{c}}
                        </div>
Please assist in resolving my problem

This is Angular HTML code block

Answer №1

Assuming you have an array of objects that requires iteration.

First Strategy

Filter the array to display only the desired items. Utilize the Array.filter method for this purpose.

Second Strategy

Develop a custom pipe to manage the logic and determine whether to return true or false to *ngIf. Modify your code accordingly:

@Pipe({
  name: 'resolve',
  pure:true
})
export class ResolvePipe implements PipeTransform {

  transform(bookstartTime: any, offerStartTime: any, bookendTime: any, offerEndTime: any): boolean {
    if (bookstartTime < offerStartTime) {
      return bookendTime > offerEndTime
    } else if (bookstartTime > offerEndTime) {
      return bookendTime < offerEndTime
    } else {
      return true;
    }
  }
}
// In the HTML
<div 
*ngIf="resolve | 
data.bookstartTime:data.offerStartTime:data.bookendTime:data.offerEndTime">
hello
</div>

[NOTE] Remember to include your own typings and ensure to add this pipe to your ng module declarations array when opting for the second strategy.

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

Guide to implementing real-time filtering for a table through user input in a text box

https://i.stack.imgur.com/6DTAb.jpgI have a task to complete for an assignment. The assignment requires implementing client-side filtering of data as text is entered in a text box. I need guidance on how to implement it. Below is the problem statement for ...

What is the most appropriate form to use, and what factors should be considered in determining

Incorporating generics in typescript allows me to create a generic function in the following manner: Choice 1 declare function foo1<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; } Alternatively, I have the option to omit the seco ...

What is the correct way to invoke a static TypeScript class function in JavaScript?

Recently, I encountered a scenario where I have a TypeScript script called test.ts structured like this: class Foo { public static bar() { console.log("test"); } } The requirement was to call this TypeScript function from plain JavaScript ...

Angular reactive form encountered an issue with an incorrect date being passed

Currently, I am utilizing the PrimeNg calendar module to select a date. Here is the code snippet: <p-calendar formControlName="valid_till" [dateFormat]="'mm/dd/yy'"></p-calendar> Upon selecting a date like 31st J ...

There seems to be an issue with the API response being undefined when displayed on

After retrieving a list of items from my MySQL database, I noticed that when I click the 'edit' button by ID, the fields are not being filled with the data as expected. Instead, I am seeing 'undefined' in my console. Interestingly, when ...

Error: The function split cannot be used on the token

I encountered an unexpected error while logging in and despite my efforts over the past 2 days, I have not been able to resolve it. The API is built using Nodejs and Angular 7 is used for the frontend. I have shared some of the code snippets with the erro ...

I'm looking to switch out the `~` to turn it into a URL for the backend, seeing as `<img alt="" src="~/media/Banner/plane.JPG">` is returning a 404 error

1. Received an HTTP Request and JSON response from the backend localhost:3000 (entered value using wysiwyg) { "Description": "&lt;img alt=&quot;&quot; src=&quot;~/media/Banner/plane.JPG&quot; /&gt;1 test berita&lt ...

Encountering compilation errors with Angular project following installation of a package

I recently installed the LocalStorage Package and encountered the following message: npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfem ...

Tips for stopping TypeScript code blocks from being compiled by the Angular AOT Webpack plugin

Is there a way to exclude specific code from Angular's AOT compiler? For instance, the webpack-strip-block loader can be utilized to eliminate code between comments during production. export class SomeComponent implements OnInit { ngOnInit() { ...

Changing the method signature in a TypeScript interface to replace/override the original one

For this specific scenario, the Array<T> interface is being extended in the following manner: interface BetterArray<T> extends Array<T> { push(this: BetterArray<T>, value: T): this; } Important note - the implementation of Arr ...

Making if-else statements easier

Greetings! I have a JSON data that looks like this: { "details": { "data1": { "monthToDate":1000, "firstLastMonth":"December", "firstLa ...

How to remove the border of the MUI Select Component in React JS after it has been clicked on

I am struggling to find the proper CSS code to remove the blue border from Select in MUI after clicking on it. Even though I managed to remove the default border, the blue one still persists as shown in this sandbox example (https://codesandbox.io/s/autumn ...

How to automatically redirect in Angular 2 by utilizing router.navigate

router.navigate() seems to be working correctly in one scenario, but in another case it is redirecting to the home page unexpectedly. app.module.ts const appRoutes: Routes = [ { path: 'news/:urlLink', component: ArticlereaderComponent }, ...

Discover the steps to dynamically set global data in Vue during runtime

I am currently working on a Vue application that requires fetching data from JSP at runtime, which means I cannot use .env files. As a solution, I am attempting to set data in Vue that can be accessed throughout the entire application (components, mixins, ...

An error occurred: Unable to access the 'basemapLayer' property due to it being undefined

Oops! TypeError: Unable to access 'basemapLayer' property of undefined I recently put together a simple application using the Angular CLI. After successfully building and running the application with ng serve -o, I encountered an issue in Chrome ...

Encountering a problem with Typescript and eslint while utilizing styled-components and Material UI: "Warning: React does not identify the `showText` prop on a DOM element."

While using a styled component to return a material ui Fab component, an error appears in the console: React does not recognize the `showText` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as low ...

Typescript: Retrieve an interface containing properties that are found in interface A, but not in interface B

I am currently developing a mapper that will facilitate the translation between a serialized entity state and a form state. In the context of two given interfaces A and B, I am exploring ways to derive a third interface C that includes properties present ...

"The debate over using 'stringly typed' functions, having numerous redundant functions, or utilizing TypeScript's string enums continues to divide the programming

I have a specific object structure that contains information about countries and their respective cities: const geo = { europe: { germany: ['berlin', 'hamburg', 'cologne'], france: ['toulouse', ' ...

Tips to store Google fonts in the assets directory

I've included this link in my styles.scss @import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&display=swap'); While it works locally, the API fails on production or is blocked. How can I host it within my p ...

What is the process for loading a model using tf.loadModel from firebase storage?

I'm currently working on an app using the ionic3 framework that recognizes hand-drawn characters. However, I am encountering difficulties with importing the model into my project. The model was initially imported from Keras and converted using tensorf ...