Send an array object to ng-template using ng-for

Angular version:

Angular: 5.2.1
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.6.5
@angular-devkit/build-optimizer: 0.0.41
@angular-devkit/core: 0.0.28
@angular-devkit/schematics: 0.0.51
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.5
@schematics/angular: 0.1.16
typescript: 2.5.3
webpack: 3.10.0

Encountering an issue when passing an object from ng-for to ng-template; receiving the error 'Can't bind to 'ngTemplateOutletContext' since it isn't a known property of 'ng-container'. 1. If 'ngTemplateOutletContext' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.

The CommonModule has already been imported in my app.module

import { CommonModule } from '@angular/common';
@NgModule({
  declarations: [
    ....
  ],
  imports: [
    CommonModule,
    BrowserModule,
    ...
    ]


    <tr grid-item *ngFor="let row of environs; let i = index">
        <td width="30%">{{i+1}} ). {{row.envName}}</td>
        <ng-container *ngIf="row.services?.length > 0">
            <ng-container *ngTemplateOutlet="deviceBlock" 
                [ngTemplateOutletContext]="{svc:row.services[0]}"></ng-container>
        </ng-container>

        <div *ngFor="let dev of row.services; let j = index">
            <ng-container *ngTemplateOutlet="deviceBlock" 
                [ngTemplateOutletContext]="{svc:dev}"></ng-container>
        </ng-container>
        </div>
    </tr>
<ng-template #deviceBlock let-svc='svc'>
    <td width="30%">{{svc.deviceName}}</td>
    <td width="20%">{{svc.serviceName}}</td>
</ng-template>

Answer №1

Great news, I managed to resolve the issue!


        <ng-container [ngTemplateOutlet]="deviceBlock" 
            [ngTemplateOutletContext]="{svc:row.services}"></ng-container>
        <div *ngFor="let dev of row.services; let j = index">
                <ng-container [ngTemplateOutlet]="deviceBlocks" 
                    [ngTemplateOutletContext]="{dev:dev}"></ng-container>
        </div>

<ng-template #deviceBlock let-svc='svc'>
    <ng-container *ngIf="svc?.length > 0">
            <ng-container [ngTemplateOutlet]="tableRow" 
                [ngTemplateOutletContext]="{svc:svc[0]}"></ng-container>
    </ng-container>
</ng-template>
<ng-template #deviceBlocks let-dev='dev'>
    <tr>
        <td>&nbsp;</td>
        <ng-container [ngTemplateOutlet]="tableRow" 
        [ngTemplateOutletContext]="{svc:dev}"></ng-container>
    </tr>
</ng-template>
<ng-template #tableRow let-svc='svc'>
    <td width="30%">{{svc.deviceName}}</td>
    <td width="20%">Ip Address</td>
    <td width="20%">{{svc.serviceName}}</td>
</ng-template>

UPDATE: (Additional note) I want to express my gratitude for the solution shared here

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

Adjust the width of Google chart to 100% automatically when the page is resized in Angular version 4 or higher

My application has numerous responsive wrappers on the site, each predefined from an API and containing a Google chart. The problem is that when the page initially loads, the charts are rendered at a specific size, and resizing the window only affects the ...

Validation of input type number restricted to only numeric values

Is there a way to validate an input field with type="number" using only Reactive Forms in Angular, ensuring that the value is either numeric or null without utilizing directives? Only numbers [0-9] and . are permitted, no other characters. My Approach: ...

Provide a definition for constraining type parameters in the inclusion of static methods

Is it possible to set type parameter restrictions for a serializer where the type must have a specific static member or implement an interface "statically"? For example: function getStuff<T>(...) -> T { T.buildFormJson(getStuffJson(...)) } He ...

Is it possible to transform a ReadonlyArray<any> into a standard mutable array []?

At times, when working with Angular functions and callbacks, they may return a ReadonlyArray. However, I prefer using arrays in the traditional way and don't want to use immutable structures like those in Redux. So, what is the best way to convert a ...

What are the steps for integrating TypeScript code into a Vue component?

https://github.com/bradmartin/nativescript-texttospeech This texttospeech package has TypeScript documentation available. Is there a way to convert this code for use in NS-Vue? import { TNSTextToSpeech, SpeakOptions } from 'nativescript-texttospeec ...

Transforming an array of HTTP Observables into an Observable that can be piped asynchronously

I am attempting to execute a series of XHR GET requests based on the results of an initial request. I have an array of observables representing the secondary requests I wish to make, and I am able to utilize Array.map for iterating over them and subscribin ...

Obtaining API/JSON Data to Implement in a NextJS Application

Currently, I am working on developing a NextJs website that focuses on detecting crop diseases. The process involves sending photos and environmental data to a fastapi python server for processing. Subsequently, the processed data is supposed to be display ...

NextJs 13 unveils its cutting-edge dynamic sitemap feature

I am currently working on implementing a dynamic sitemap for NextJs 13.4.6, referring to the guide available at . However, I have encountered an issue with the code provided towards the end of the article which is supposed to generate a sitemap for NextJS ...

Check if the array includes a specific index

Below is the code snippet I am currently working with: <li *ngFor="let item of serviceList; let i = index;"> <button *ngIf="currentTools contains i" (click)="processService(item)"> Run Service</button> </li> Is t ...

Opt for ion-select with a range of i to j options

Looking to set up an ion-select menu in Ionic4 where users can pick a value between 20 and 220 without manually typing each number. I attempted to use the approach detailed in this post Tersest way to create an array of integers from 1..20 in JavaScript ...

What is the best way to begin at a specific index in an array without skipping any elements?

As I continue my journey of learning JS, I have encountered a task that has left me quite perplexed. The objective is to provide an index to start from and also include the items missing in the array while displaying them in an angular format. Here is what ...

Ensure the privacy of your app with Ionic secure storage by prompting the user to establish a personalized lock

I'm running into an issue while trying to set up the secure storage plugin. If initialization fails, it typically indicates that the user hasn't configured a secure lock screen. Following guidelines from the project's GitHub page, I am attem ...

Access a .docx file on my device by utilizing the Microsoft JavaScript API

In my current project, I am developing a Microsoft Word add-in using TypeScript, React, and the Word API. One of the key features of this add-in will allow users to open a document located on their computer, such as "C:\Test\Test.docx", with just ...

What is the reason behind the prevalence of using export class xxx in angular4 projects instead of export default class xxx?

Understanding the distinction between export class xxx and export default class xxx is crucial. However, I've noticed that numerous angular4 projects utilize export class xxx in this manner: @Component({ selector: 'call-record', templ ...

Configuration error encountered in a project utilizing Angular CLI, Firebase, and Travis CI with a project-based approach

I've been having some issues with my travis.yml file code. No matter how many times I edit it, my Travis CI project keeps failing repeatedly. .travis.yml language: node_js node_js: - "8.9" branches: only: - master before_script: ...

Guide to setting a generic key restriction on a function parameter

Today, I decided to have some coding fun and try creating a generic pushUnique function. This function is designed to check if a new object being added to an array is unique based on a key, and then push it if it meets the criteria. At this point, all I h ...

How to use jsZIP in angular to bundle several CSV files into a single zip folder for download

I have a code snippet below that demonstrates how to create data for a CSV file and download the CSV file in a zip folder: generateCSVfiles() { this.studentdata.forEach(function (student) { this.service.getStudentDetails(student.studentId).subsc ...

What is the proper procedure for invoking a delete method to remove all occurrences of a specific index?

I have implemented a file deletion method in my Ionic 4 App. Using the deleteFile() method, I can successfully delete individual files. However, when attempting to delete multiple files using a forEach loop, the process fails after the first deletion. HTM ...

What could be causing the lack of updates to my component in this todo list?

Based on my understanding, invoking an action in MobX should trigger a rerender for the observer. However, when I call the handleSubmit method in my AddTask component, it doesn't cause the TaskList observer to rerender. Should I also wrap AddTask in a ...

The issue of saving Rails input from an Angular2 front end has been causing some trouble

Currently, I am in the process of developing a front-end application using Angular 2 that retrieves data from a Rails 5 API. This specific application serves as a network inventory tool. One particular feature of this app is an Asset-form in Angular2 whic ...