The @Input directive is not compatible with the OnPush change detection strategy

page.html

<app-parcel-delivery-cost-promo [parcelDeliveryCost]="parcelDeliveryCost">
  </app-parcel-delivery-cost-promo>

page.ts

changeDetection: ChangeDetectionStrategy.OnPush,

parcelDeliveryCost: Partial<ParcelDeliveryCostModel>;

  ngOnInit(): void {
     this.translateService.get(['Client.Parcel-Delivery-Cost']).subscribe(res => {

      this.parcelDeliveryCost = {
        title: res['Client.Parcel-Delivery-Cost'],
        description: res['Client.Parcel-Picked-Up-From-The-Location-And-Delivered-To-You'],
        cost: environment.parcelDeliveryCost
      };
    });
  }

component.ts

 changeDetection: ChangeDetectionStrategy.OnPush,

export class ParcelDeliveryCostPromoComponent implements OnInit {

  @Input() parcelDeliveryCost: ParcelDeliveryCostModel;

  constructor() { }

  ngOnInit(): void { }

}

componet.html

<ion-grid>
  <ion-row>
    <ion-col size="12" >
      <h5 class="font-bold margin-top-bottom-5">{{parcelDeliveryCost?.title}}</h5>
    </ion-col>
    <ion-col size="12">
      <div class="color-medium">{{parcelDeliveryCost?.description}}</div>
    </ion-col>
    <ion-col size="12">
      <h4 class="margin-top-bottom-5 text-line-through">{{parcelDeliveryCost?.cost}}</h4>
    </ion-col>

  </ion-row>
</ion-grid>

Q: Can you explain the behavior of @Input not working with OnPush change detection in this code? What could be causing the issue, such as the app-parcel-delivery-cost-promo component not displaying until a page modification is made?

Answer №1

When using input, everything seems to work smoothly. However, when implementing Subscription in ngOnInit with the OnPush strategy, the change detection does not get triggered as expected. To resolve this issue, consider one of the following options:

  • Manually run the change detector within the subscription.
  • Replace .subscribe() with the async pipe (which automatically runs markForCheck).
  • Disable the OnPush strategy altogether.

Answer №2

Give this a shot

 let updatedParcelDeliveryCost = Object.assign({},{
        title: res['Client.Parcel-Delivery-Cost'],
        description: res['Client.Parcel-Picked-Up-From-The-Location-And-Delivered-To-You'],
        cost: environment.parcelDeliveryCost
      });

Angular triggers change detection when the reference of an object is modified.

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

What is the best way to outline the specifications for a component?

I am currently working on a TypeScript component. component @customElement("my-component") export class MyComponent extends LitElement { @property({type: String}) myProperty = "" render() { return html`<p>my-component& ...

I am encountering an issue where the $scope.modal is returning as undefined when attempting to open an Ionic template modal. What

I am currently developing an Ionic application for the iPad. Within one of my templates, I have implemented a treeview with items. My goal is to open other templates modally when users click on these items. However, I encountered an error stating that $sco ...

Out of nowhere, encountering TS2322 Typescript errors, causing type mismatches during the compilation phase

I am facing an issue with AWS Codebuild while deploying APIs written in lambda and exposed via API Gateway using Koa. The build process is throwing an error related to type assignment. src/components/chart-color-settings/chart-color-settings.ts(11,13): err ...

Angular Universal causing issues with updating the DOM component

@Component({ selector: 'mh-feature-popup', template: ` <div class="full"> <div> <div class="container-fluid" [@featurepop]="state"> <div class="row"> <div class="col-xs-12 col-md-4 col-md-offse ...

Getting js.map Files to Function Properly with UMD Modules

I am experiencing an issue with debugging TypeScript files in Chrome and Firefox. Specifically, when trying to debug the MapModuleTest.ts file, the debugger seems to be out of sync with the actual JavaScript code by two lines. This discrepancy makes settin ...

Obtain the total number of requests submitted by the user within a 24-hour period

I have a POST request point in my API where I need to track and store all work journals made by a worker. export const registerPoint = async (req: Request, res: Response) => { const user = res.locals.decoded; const {id} = user; const point = new Point ...

Transferring data between unrelated components

I am facing an issue where I am unable to pass a value from the Tabs component to the Task component. To address this, I have created a separate data service. The value in the Tabs component is obtained as a parameter from another component. However, when ...

Guide to sending jQuery data back to main class in TypeScript

Hi everyone, I'm diving into the world of typescript and JQuery. I have a simple question. In my typescript class called DatePicker, I am calling a function. Here's a snippet of the code: Class DatePicker { private pickerData; public update( ...

The functionality of Angular material input fields is compromised when the css backface property is applied

I utilized this specific example to create a captivating 3D flip animation. <div class="scene scene--card"> <div class="card"> <div class="card__face card__face--front">front</div> <div class="card__face card__face--ba ...

What are the steps to incorporating ng2-dnd with a background-image?

I am currently utilizing the ng2-dnd module to enable sorting of a list. While the functionality for sorting and dragging is working smoothly, there's one issue - users can only drag by clicking on the text within my element. My goal is to allow user ...

Filter through the array using the cast method

Trying to implement this: let selections = list.filter(obj => obj.type === "myType"); An issue arises with the filter function displaying an error message which states 'filter' does not exist on type 'NodeType' I attempted to ...

Converting time from 00:00:01 to a format of 8 minutes and 49 seconds in Angular

Is there a way to transform a time value from 00:00:01 (not a date object) into a format showing 8 minutes and 49 seconds? Even after consulting the Angular 'date pipe' documentation, I couldn't find a solution to this issue. ...

Leveraging Angular 4 with Firebase to extract data from a database snapshot

My dilemma lies in retrieving data from a Firebase DB, as I seem to be facing difficulties. Specifically, the use of the "this" operator within the snapshot function is causing issues (highlighted by this.authState.prenom = snapshot.val().prenom) If I att ...

Forwarding refs in React FC allows you to easily pass down

I have encountered an issue with references - I am trying to reference a function component and pass props to it. Currently, I have my Parent component and Child Component set up. In the parent component, I need to use a ref to access my child component. S ...

Exploring date formatting in NestJs with Javascript

Currently, I am working with a ScrapeResult mikroOrm entity. I have implemented the code newScrapeResult.date = new Date() to create a new Date object, resulting in the output 2022-07-17T17:07:24.494Z. However, I require the date in the format yyyy-mm-dd ...

Can you provide details about the launch configuration for pwa-node in VSCode?

When setting up npm debugging in VSCode, I couldn't help but notice that the default launch configuration is labeled as "pwa-node" instead of just "node". Here's what the "Launch via NPM" configuration looks like: And this is what the generated ...

The `ngx-infinite-scroll` feature does not function properly on mobile devices when used in conjunction with the

I'm currently working on an Angular project that utilizes the mat-sidenav layout and routing. I came across an issue when trying to display a list of elements from a database using ngx-infinite-scroll. Following the user guide for ngx-infinite-scroll, ...

Guide on embedding child components in content using ngx-markdown within Angular version 14

I've been struggling to find a solution to this issue for some time now. We are receiving HTML content from an epi-server that needs to be dynamically rendered in a component. While ngx-markdown works well with standard HTML elements, it seems to ign ...

The speed of Mean Stack API calls leaves much to be desired

Our team has developed a MEAN stack application that functions smoothly on the client side. However, we have been facing issues with slow API requests and responses on the server side. For example, when a request is sent from Angular, it takes an average ...

Pause for Progress - Angular 6

I'm looking for a solution to solve the following issue. I need to access a property that will store data from an http request. Therefore, I want to verify this property only after the transaction is completed. validateAuthorization(path: string): ...