What is the best way to utilize ngForTemplate for wrapping a template?

Using the ngForTemplate in a ListView component for custom templates has proven to be challenging.

Modified list-view.component.html:

<div class="list-view">
    <template ngFor [ngForOf]="items" [ngForTemplate]="template">
    </template>
</div>

Updated list-view.component.ts:

@Component({
    selector: 'm-list-view',
    styleUrls: ['./list-view.component.less'],
    templateUrl: './list-view.component.html'
})

export class ListViewComponent {
    @Input() items: any[] = [];

    @ContentChild(TemplateRef)
    template: any;
}

One issue arises when trying to implement it with a structure like this:

<m-list-view [items]="categories">
    <template let-item="$implicit">
        **some HTML markup**
    </template>
</m-list-view>

The output ends up being different from what is expected, which should look like this instead:

<m-list-view>
    <div class="list-view">
        <div class="list-view-item">**some HTML markup**</div>
        <div class="list-view-item">**some HTML markup**</div>
        <div class="list-view-item">**some HTML markup**</div>
        ...
    </div>
</m-list-view>

In order to achieve the desired result of wrapping each list view item template in div.list-view-item, further adjustments need to be made.

Answer №1

Upgrade to Angular 5

ngOutletContext has been changed to ngTemplateOutletContext

It is now recommended to use the following syntax:

<div class="list-view">
  <div class="list-view-item" *ngFor="let item of items">
    <ng-container *ngTemplateOutlet="template; context: item">
    </ng-container>
  </div>
</div>

For more information, visit https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29

original

<div class="list-view">
  <div class="list-view-item" *ngFor="let item of items">
    <template [ngTemplateOutlet]="template" [ngOutletContext]="item"></template>
  </div>
</div>

For further details, check https://angular.io/docs/ts/latest/api/common/index/NgTemplateOutlet-directive.html

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

Error: Unable to access the 'dispatch' properties as it is undefined in useShoppingCart function

Currently, I am utilizing the Stripe API along with use-shopping-cart for an e-commerce platform. However, I have encountered a perplexing issue that I cannot seem to resolve. When I wrap a page with CartProvider, that specific page successfully interacts ...

Angular 2: Enhancing the table with Bootstrap Modal behind the scenes

I have a roster of students that I can manipulate using CRUD functions. Each student entry has an Edit button <table class="table table-bordered table-striped"> <thead class="studentHeader"> <tr> <td>Roll Number</t ...

Drag and drop files effortlessly with Angular framework

Just getting started with Angular and typescript. I need to be able to select single or multiple video files by dragging and dropping using Angular. I found a helpful reference here: https://stackblitz.com/edit/angular-rksspi?file=src%2Fapp%2Fapp.componen ...

The browser successfully navigates in the e2e test, but the URL cannot be matched

Currently diving into the world of e2e testing with Protractor in our Angular application. My current focus is on testing the routing within a specific page. The goal here is to create and trigger a page object that will click on the top row of test data w ...

Working with Union Types in the state of React's Context API using TypeScript

I'm facing an issue where TypeScript is not recognizing the existence of the property state.recipes when I use the state in another component. This seems to occur when YummlyState is the type of RecipesState. I have a hunch that YummlyState always def ...

Top method for sending arguments when utilizing RxJS forkJoin

Hey there! I'm currently working on a service with three functions that I need to execute. To handle this, I've opted for using forkJoin as it allows me to take further action once all responses are received. One of the functions requires one par ...

Transferring information between two pages when a button is clicked in Angular

How can I display hero details on the hero-details page when clicking a button? I'm currently struggling to make it work. When I click the button, the details show up on the hero-list page, but using the routerLink doesn't display the data. I am ...

Modify the background color of a clicked event in Angular Full Calendar

Is it possible to modify the background color of an event when it is clicked on in Angular FullCalendar? I attempted to do so by setting the event backgroundColor property within the click event, but the changes were not reflected in the view. eventClick ...

An error was encountered in the rxjs-compat module at operator/shareReplay.d.ts line 2, character 10: TypeScript error TS2305

Currently, I am in the process of upgrading a basic Angular skeleton application from version 5 to version 6. However, I have encountered an issue while attempting to run the application: ERROR in node_modules/rxjs-compat/operator/shareReplay.d.ts(2,10): ...

The rendering of code is often disrupted when utilizing the keyword const

I've been working my way through the Angular2 tutorial called Tour of Heroes and everything has been going smoothly up until this point. At the link above, I've encountered a problem. The code on the left is what the tutorial recommends, but fo ...

Transferring a data object to a Spring REST endpoint using Angular 5

Having an issue with uploading a file on the client side and sending an HTTP Post request to a Spring Rest Controller. The problem arises when the file object is received in the Rest controller, as it appears to be 'null'. Here is the code snippe ...

Downloading Excel from Web API with Angular 2 Service

There is an API end-point located at http://localhost:59253/api/reports?reporttype=evergreen. When pasted in the browser, it successfully downloads an excel file in (.xlsx) format. I am now attempting to access this end-point from my Angular service. Bel ...

The NextRouter failed to mount in Next.JS

When you use import { useRouter } from "next/router"; instead of import { useRouter } from "next/navigation";, it results in the error message "Argument of type '{ pathname: string; query: { search: string; }; }' is not assign ...

Issue with HTTP interceptor failing to activate

I'm having trouble with my interceptor not triggering. Despite trying multiple solutions, I can't seem to get it working. The interceptor is failing to intercept http requests, resulting in the 'Authorization' header not being added. ...

Can ngFor be utilized within select elements in Angular?

I'm facing an interesting challenge where I need to display multiple select tags with multiple options each, resulting in a data structure that consists of an array of arrays of objects. <div class="form-group row" *ngIf="myData"> <selec ...

Using the useStaticQuery hook outside of a function component is not allowed and will result in an invalid hook call error. Remember to only call

I am currently facing an issue while trying to retrieve values using useStaticQuery from my gatsby-config.js file. Below are snippets of my code. Does anyone have any suggestions on how to resolve this problem? Thank you in advance. Repository: https: ...

The mark-compacts were not efficient enough, they approached the heap limit and as a result, the allocation failed. The JavaScript

Currently working with Angular version 7.2 and encountering an issue when running ng serve: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory What does this error mean? How can it be resolved? The ...

The p-confirmDialog is experiencing issues with its display

One of the buttons using p-confirmDialog is causing an issue on my page. It appears that the confirmation is triggered by a button on a popup, causing the confirmation to appear behind it and be grayed out. https://i.sstatic.net/MyUJO.gif HTML Interes ...

Confirm Ng2 password and verify.password

These are the codes I've been working with: <form [formGroup]="registerForm" novalidate (ngSubmit)="doRegister()"> <div class="form-group" [ngClass]="{'has-danger': registerForm.control ...

Capturing Angular 4 Screenshots with html2canvas

Is there a way to capture and send a screenshot using html2canvas in Angular 4 via an HTTP POST request? Component import { Component, OnInit, NgZone } from '@angular/core'; import { Router, ActivatedRoute, Params } from '@angular/rout ...