The ng-template directive does not duplicate data, whereas HTML elements have the ability to showcase it

When trying to display JSON data using ng-template, I am facing issues as the data is not showing up. However, if I use HTML elements like div or span, it displays correctly.

JSON format:

const arr = [ { "date": 1, "color": "normal" }, { "date": 2, "color": "red" }, { "date": 3, "color": "normal" } ]

In TypeScript:

dateArray: any;

public getJSON() {
     return this.http.get("../assets/eventcalendar.json")
        .pipe(map(res => res.json()));
  }

  getCalendar(){
    this.getJSON()
    .subscribe((event:any)=>{
        let getEvent = JSON.stringify(event);
        this.dateArray = JSON.parse(getEvent);
        console.log('this.dateArray', this.dateArray);
    })

  }

In HTML: (This template fails to show the JSON data)

<p-calendar (onSelect)="select($event)" [inline]="true" selectionMode="multiple" readonlyInput="true">
  <ng-template pTemplate="body" let-item="dateArray">
    <span 
      [ngClass]="item.color">
      {{item.tgl}}
    </span>
  </ng-template>
</p-calendar>

In HTML: (This template successfully shows the JSON data but has UI issues with the calendar)

<p-calendar (onSelect)="select($event)" [inline]="true" selectionMode="multiple" readonlyInput="true">
  <ng-template pTemplate="body">
    <span *ngFor="let item of dateArray"
      [ngClass]="item.color">
      {{item.tgl}}
    </span>
  </ng-template>
</p-calendar>

I am utilizing primeng calendar for the calendar and working on Angular 7 latest version. Thank you

Answer №1

@Alkim Al, give this a try

replace it with

<ng-template pTemplate="body" let-item="dateArray">

next line should be

<ng-template ngFor let-item [ngForOf]="dateArray">

this might help you solve the problem. For more information, refer to this link

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

Function input custom operator in RxJs

I am currently working on developing a custom rxjs operator. My previous custom operators, such as MonoTypeOperatorFunction or the regular Observable that accepts input like strings or numbers, have been successful. However, I am facing a challenge with cr ...

Using React to iterate through a map and render various components

There's a unique scenario I'm facing, In my current environment, I don't have a standard list structure that allows me to map through and display components in each iteration, like this: list.map(shape => <Shape {...shape} />) In ...

Displaying JSON data using FormControls in Angular 5

Upon receiving Json values from the server, I am encountering an issue while binding them to respective textboxes. The problem arises as the value in the textbox appears as [object object] <h1>{{title}}</h1> <h3>Catalog</h3> ...

Implementing Caching in Angular 5 Services

Searching for the best way to implement Angular services has led me here. The Service: const url = 'http://127.0.0.1:8000/api/brands/' @Injectable() export class BrandService { private brands:Observable<Array<Brand>>; constru ...

Angular 8 delivers an observable as a result following a series of asynchronous requests

I am working on a simple function that executes 3 asynchronous functions in sequence: fetchData() { this.fetchUsers('2') .pipe( flatMap((data: any) => { return this.fetchPosts(data.id); }), fl ...

Tips for restricting additional input when maximum length is reached in an Angular app

In my Angular 6 application, I am working on implementing a directive that prevents users from typing additional characters in an input field. However, I want to allow certain non-data input keys such as tab, delete, and backspace. Currently, I have an if ...

Exploring the power of flow.js within an Angular 2 Typescript project

I am trying to incorporate flowjs or ng-flow into my Angular 2 application. After installing the flowjs typings using npm install --save-dev @types/flowjs from However, upon importing it into my component with import { Flow } from 'flowjs';, ...

The module "install-npm-version" could not be located

I am currently working on a project using TypeScript, which you can find at this GitHub repository. However, when I attempt to use the package in another project, I encounter an error that says Cannot find module 'install-npm-version'. Steps to ...

Angular Karma Error - MatDialogRef Provider Not Found

While testing with Angular Karma, I encountered the following error... NullInjectorError: StaticInjectorError(DynamicTestModule)[ManageProblemsComponent -> MatDialogRef]: StaticInjectorError(Platform: core)[ManageProblemsComponent -> MatDialogRef]: ...

What steps can I take to detect errors within AWS Amplify?

Examining the code snippet below React.useEffect(() => { Auth.currentUserInfo() .then((data) => { if (data.username) { //do something with data } }) .catch((error) => console.log('No logged in ...

What is the best way to simulate mailgun.messages().send() with Jest?

Currently, I am utilizing the mailgun-js Api for sending emails. Instead of a unit test, I've created an integration test. I am now facing the challenge of writing a unit test case for the sendEmail method within the Mailgun class. I am unsure of how ...

Tips for incorporating flow and TypeScript typings into an NPM module

Are there any resources available for adding both flow and typescript typings to an NPM module at the same time? I've been struggling to find a comprehensive guide on this topic, and it seems to be a common issue faced by open source library maintain ...

Prevent a specific directory from being compiled in MSBuild using Typescript

I have a unique ASP.NET MVC / Angular2 project that utilizes MSBuild for compiling my Typescript files. Within the project, there is the complete Angular2 source code along with its NodeJS dependencies, in addition to my own Angular2 code (app.ts, etc.). ...

Having difficulty sending the [ctrl][p] keys to a page that is not using Angular framework

Working with protractor version 5.1.2, Angular 5, and typescript 2.4.2 When attempting to trigger a 'print' function using the shortcut keys '[ctrl][p]' in protractor on a non-angular page, I encountered an issue. Within my protractor ...

Tips for resolving the error message "Cannot use type '{}' as an index type"

interface TicketTime { startTime: string; endTime: string; } interface TicketInfo { id: number; from: string; to: string; company: string; price: number; time: TicketTime; duration: number; connectionAmount: numb ...

Examining the asynchronous function to cause an error using mocha

I am facing a challenge with testing an async function that is supposed to run for 2000ms before throwing an exception. Despite my efforts using Mocha / chai, the test does not seem to be working as expected. Here's what I have attempted: First appr ...

Use TypeScript types to specify the types of props passed into a React component for better type safety and clarity

Struggling to extract the value passed to a prop in my react component, and use it as a type for other props within the same component. const TestPage = () => { return ( <Test tabs={[ { label: "test-label", value: " ...

How can I store unique and only selected checkbox values in an array using Angular?

I need assistance with creating an array from three checkboxes. The array should only contain the values of the checked checkboxes and should not include duplicates. I have attempted to achieve this functionality, but the values are still being added rega ...

Can a TypeScript generic version of the Y-combinator be successfully executed?

Here is an interesting JavaScript implementation of the Y-combinator: const Y = g => (x => g(x(x)))(x => g(x(x))) //or const Y = f => { const g = x => f(x(x)) return g(g) } I've been thinking, could it be possible to create a TypeS ...

In TypeScript, the catch block does not get triggered

I created a custom pipe in Angular that is supposed to format passed parameters to date format. The pipe contains a try-catch block to handle any errors, but surprisingly the catch block never seems to be executed even when an invalid date is passed. impo ...