Skip to content

ChangeDetectionStrategy.OnPush not running properly with @Input modified programmatically (outside of a template binding)  #20611

Closed
@UserGalileo

Description

@UserGalileo

I'm submitting a...


[X] Bug report  

Current behavior

I'm trying to set an Input property of a component which is projected into another one. I tried, for performance reasons, to use ChangeDetectionStrategy.OnPush on the child component, but I've come across a strange bug.
With ChangeDetectionStrategy.Default everything works fine, but using OnPush I get a strange behavior. It appears that the OnPush component doesn't receive the new input property. Please see my example which clarifies the problem.
I think the problem could be confined in ContentChildren, ViewChildren (same problem) or QueryList, and I believe it's the latter but I'm not sure about it.
It's been clarified that OnPush just looks at inputs which are set in the template and not via JavaScript. Although this kinda makes sense thinking about the current framework's logic, it doesn't in a sense that the child component using OnPush isn't "thinking with its own brain", but it's the parent who decides if re-rendering should occur for it, based only on its template bindings.

Expected behavior

The component's Input property should be received and the component re-rendered even with ChangeDetectionStrategy.OnPush.

Minimal reproduction of the problem with instructions

This is the demo I made, with a simple component which is responsible for the state of all its components which are being projected inside of it.

https://stackblitz.com/edit/angular-accordion

Please see the comment in toggle.component.ts and try changing Default to onPush

What is the motivation / use case for changing the behavior?

Compound components often need to be used with OnPush in order to optimize performance with large applications.

Environment


Angular version: 5.0.0


Browser:
- [X] Chrome (desktop) version 62

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions