Using the slice pipe on the data for a child component property is resulting in endless calls to the @Input set method

After incorporating a slice pipe into the data object below and passing that data to the child component's @Input method, there appears to be an endless loop of calls to that method. However, eliminating the slice pipe from the data object resolves this issue, as the @Input method is only triggered once.

Template of the Parent component:

<ng-container
  *ngIf="[
      { name: 'a', order: 1 },
      { name: 'b', order: 2 },
      { name: 'c', order: 3 },
      { name: 'd', order: 4 },
      { name: 'e', order: 5 }] | slice:0:3 as data"
>
  <slice-pipe-test [testData]="data"></slice-pipe-test>
</ng-container>

Typescript code of the Child component:

  @Input()
  set testData(testData) {
    console.log(testData);
  }

Above link displays the infinite calls in the console:

Output

For full sample code (ensure to add " | slice:0:3 " to the data object in app.component.html as depicted above, causing the endless calls and browser freeze):

https://codesandbox.io/s/3dg47  

Could this possibly be related to change detection or an impurity in SlicePipe? Moving the sliced data object into the typescript file seems to resolve the issue. Is there a way to maintain the slice in the html template without causing continuous calls to the child component's @Input method?

Answer №1

It is best practice to utilize the pipe when rendering the view.

In this scenario,

slice-pipe-test.component.html

<table>
  <tr *ngFor="let item of myData | slice:0:3">
      <td>{{item.order}}</td>
      <td>{{item.name}}</td>
  </tr>
</table>

app.component.ts

this.data = [
  { name: 'a', order: 1 },
  { name: 'b', order: 2 },
  { name: 'c', order: 3 },
  { name: 'd', order: 4 },
  { name: 'e', order: 5 }
]

app.component.html

<ng-container *ngIf="data">
      <slice-pipe-test [testData]="data"></slice-pipe-test>
</ng-container>

Alternatively, you can use slicing in Typescript.

this.data = [
  { name: 'a', order: 1 },
  { name: 'b', order: 2 },
  { name: 'c', order: 3 },
  { name: 'd', order: 4 },
  { name: 'e', order: 5 }
].slice(0, 3);

Template:

<ng-container *ngIf="data">
      <slice-pipe-test [testData]="data"></slice-pipe-test>
</ng-container>

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 message: Trying to import a component in Angular, but encountering a message stating that the component has no exported

When creating a component named headerComponent and importing it into app.component.ts, an error is encountered stating that 'website/src/app/header/app.headerComponent' has no exported member 'headerComponent'. The code for app.headerC ...

guide to utilizing npm/yarn with tsx react

I've recently made the switch to using TypeScript with React, but I'm encountering a problem. After installing certain packages from npm or yarn, I'm having trouble using them in my .tsx components. The error message suggests looking for @ty ...

Recursive type analysis indicates that the instantiation of the type is excessively deep and may lead to potential infinite loops

In the process of developing a Jest utility, I have created a solution where an object mock is lazily generated as properties are accessed or called. Essentially, when a property is invoked like a method, it will utilize a jest.fn() for that specific path ...

The property 'filter' is not recognized on the 'Object' type. An attempt to filter the response was made

Trying to fetch data from a JSON file that matches the player's name in the URL, such as localhost:4200/players/Febiven, should only retrieve information about Febiven. The code is written in Angular 6. The current code snippet is as follows: player ...

Enhance tns-platform-declarations with NativeScript

I am working on a NativeScript project and I am trying to incorporate the RecyclerView from Android Support Library. I have added the dependency in the app/App_Resources/Android/app.gradle file: // Uncomment to add recyclerview-v7 dependency dependencies ...

Unable to retrieve values from input fields that have been established using interpolation

I'm currently developing a straightforward app that involves a form with formArray. Within the formArray, users can select a product name and amount. Once both are chosen, a third input field - total - computes the total price of the items (product pr ...

Developing an npm module encapsulating API contracts for seamless integration with front-end applications using Typescript

I am curious if Typescript supports this specific idea, and I could use some advice on how to make it work. In my project, there's a frontend application and a backend REST API with clear contract classes for Inputs and Outputs. These classes outline ...

How to Publish an Angular 8 Application on Github Pages using ngh

Currently in my angular 8 project, I am encountering the following issue while running the command: ole@mkt:~/test$ ngh index.html could not be copied to 404.html. This does not look like an angular-cli project?! (Hint: are you sure that you h ...

Navigate smoothly in Angular 4 with Spring Boot without the need for hash codes in the URL routing

Here is the scenario I am facing: I am currently using Spring Boot with Angular 4 I generate a build file using angular-cli and place it under the resource -- static folder. When I run my pom.xml, all the files in the static folder are copied to target ...

typescript throwing an unexpected import/export token error

I'm currently exploring TypeScript for the first time and I find myself puzzled by the import/export mechanisms that differ from what I'm used to with ES6. Here is an interface I'm attempting to export in a file named transformedRowInterfac ...

When onSubmit is triggered, FormData is accessible. But when trying to pass it to the server action, it sometimes ends up as null

I am currently utilizing NextJS version 14 along with Supabase. Within my codebase, I have a reusable component that I frequently utilize: import { useState } from 'react'; interface MyInputProps { label: string; name: string; value: stri ...

The Angular 4 proxy configuration file is not properly directing to the specified target destination; instead, it is redirecting to localhost:4200

I'm having trouble with my Angular 4 proxy configuration file - it's not redirecting to the target I specified and instead is redirecting to <http://localhost:4200>. ...

What is the best way to retrieve the current height in VueJS using the Composition API?

I am utilizing a Ref to preserve the current height of the active element. My goal now is to transfer this height to the subsequent element that gets clicked on. <script lang="ts" setup> import { ref, reactive } from "vue"; defin ...

Guide to encapsulating an asynchronous function in a promise

I am in need of wrapping an asynchronous function within a promise to ensure synchronous execution. The reason behind this is that I must obtain a result from the asynchronous function before proceeding with the program's execution. Below is the rele ...

Angular - Sharing data between components with response value

I am currently in the process of restructuring my project, focusing on establishing communication between unrelated components while also waiting for a return value from a function call. Imagine having component1 with function1() and component2 with funct ...

Unlock the power of TypeScript by linking together function calls

I am looking for a way to create a type that allows me to chain functions together, but delay their execution until after the initial argument is provided. The desired functionality would be something like this: const getStringFromNumber = pipe() .then ...

Dropdown box not displaying any choices

I developed a basic reusable component in the following way: Typescript (TS) import {Component, Input, OnInit} from '@angular/core'; import {FormControl} from '@angular/forms'; @Component({ selector: 'app-select', templa ...

Is there a more efficient method for invoking `emit` in Vue's Composition API from an external file?

Is there a more efficient way to access the emit function in a separate logic file? This is my current approach that is functioning well: foo.js export default (emit) => { const foo = () => { emit('bar') }; return { foo }; } When ...

Overriding the 'first' attribute in PrimeNG's lazy table when implementing filtering

I encountered an issue while attempting to set up a primeNG table using query parameters. For example, when accessing , the data displayed should pertain to "Joe" and start at the 20th entry. To handle the large volume of data my backend can provide, lazy ...

Issue encountered during mozjpeg installation - unable to locate mozjpeg's cjpeg in the vendor directory due to

During my attempt to set up mozjpeg within a Docker container running NAME="Alpine Linux" ID=alpine VERSION_ID=3.11.7 PRETTY_NAME="Alpine Linux v3.11" HOME_URL="https://alpinelinux.org/" BUG_REPORT_URL="https://bugs.alpin ...