setInput() triggers ngOnChanges and mark component when it should not

This issue has been created since 2022-09-18.

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

I thought ComponentRef.setInput() will:

  • set input
  • mark component when neccessary
  • make sure ngOnChanges is called when neccessary

However it is called when OnPush component is used and input doesn't change so i think it is not correct behavior.

image

msedge_YwrPI42REg.mp4

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-yo1znz?file=src/app/hello.component.ts

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

No response

Anything else?

Of course we can check it manually, but only "component.instance" is available for the developers from "PUBLIC API" - and that's problem in case of setters without getters etc.

pkozlowski-opensource wrote this answer on 2022-09-21

Currently the setInput is not comparing prev / current values and this was conscious design decision. Something we might revisit as I've heard this need expressed elsewhere.

montella1507 wrote this answer on 2022-09-21

Hi @pkozlowski-opensource , thanks for your answer.

If i am correct, prev / current values are compared "SOMEWHERE" higher than on component level, right? In some kind of "component host container", there are stored previous inputs and ngOnChanges may be called (and component is marked) based on the result of that compare.

AFAIK we don't have "public" access to that place so we cannot do that (compare prev/current) with public api?

pkozlowski-opensource wrote this answer on 2022-09-21

ngOnChanges doesn't do prev / current comparison, this happens before input values are updated (and hence it is not implemented in the current version of the API). So it is not a question of the public API availability. Currently you would have to remember prev values and do the comparison on your side. As I've mentioned, this is sth we might consider doing inside setInput, although it is not clear what the design should be and if this is the right direction at all...

montella1507 wrote this answer on 2022-09-21

So i have to do something like

_previousName = undefined;

setName(newName: string) {
  if (newName !== this._previousName) {
   this._previousName = newName;
    this.setInput('name', newName);
  }
}

for every input (in the universal way - i can use reflectComponentType() api to get list of the inputs though)

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-09-30
Star Count 84080
Watcher Count 3064
Fork Count 22235
Issue Count 1197

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Problem when two properties have same name in nested object 0 2022-09-26 2022-09-19
The problem of the vignette during image postprocessing 27 2022-01-08 2022-07-03
Option (or default behavior) to remember last used mode (video vs. photo) 14 2022-01-07 2022-07-03
Smooth focus adjustment 2 2022-01-07 2022-08-15
AttributeError: module 'jax' has no attribute 'tree_multimap' 3 2022-08-13 2022-09-19
NoCaps test results much lower than reported val results 1 2022-08-09 2022-09-07
some unknown error during run "train_retrieval.py " 2 2022-08-22 2022-09-20
(仍存在)Openinv插件不兼容,使用/openinv报错(报错信息有变化) 0 2022-03-07 2022-09-28
Guix Package for Critic2: Request for Comments 3 2021-06-23 2022-07-17
[Gateway] Implement OnChain trading interface 1 2021-12-17 2022-08-22
unifi hold on startup 2 2021-08-16 2022-09-03
Add 'tags' parameter to ec2_lc module 2 2022-01-27 2022-07-28
CSS cannot be imported from within node_modules. 1 2021-06-16 2022-09-20
`ActorSystemSetup.And` forgets types 0 2022-03-18 2022-03-29
Vis (blauer Kreis - Keine Verbindung) States in VIS dahinter bleiben jedoch aktuell 6 2021-09-09 2022-08-30
ModuleNotFoundError: No module named 'statsmodels' in juypter notebooks 0 2022-02-04 2022-09-09
[css] 第966天 使用css3动画时(translate,scale)容易发生字体变模糊再恢复的情况如何解决? 0 2021-12-06 2021-12-23
[html] 第966天 如何在canvas中显示图片? 0 2021-12-06 2022-07-15
MMEX 1.5.17 Beta builds incorrectly labelled as 1.5.17 release 0 2022-07-10 2022-07-30
1.34导入txt小说文件,每章节最后部分不显示 3 2021-11-17 2022-08-29
Formatter removes escape slash in HEREDOC 3 2021-09-25 2022-07-17
Impaired synaptic physiology 2 2021-07-26 2022-09-26
Catto Pew Pew! (2065330) 1 2022-08-23 2022-09-06
where is there a document to refer to table **owadb.owa_action_fact;** what is the meaning of these fields? 1 2021-03-19 2022-08-01
🐞[BUG]: Websocket's plugin action are not recognized as actions 2 2021-10-18 2022-09-29
Feature Request: echo value of `sh` `:out` option 3 2020-10-01 2022-09-22
Job description file attachment 2 2021-11-18 2022-08-27
Positive Feedback -- Images for KUDOS.md 39 2016-03-24 2022-09-18
Reactive/observable field without "accessor"? 4 2022-08-04 2022-09-21
Members of group cache for multicast are never deleted 0 2022-08-30 2022-09-23
Expose API to get Config 2 2021-11-16 2022-09-27
Calculation for xDecompAgg 3 2022-08-01 2022-09-21
Issue with rstan when installing Robyn in macOS 12.5 1 2022-07-29 2022-09-21
Deploying GEL via the Jsonnet library results in license not being found 0 2021-09-01 2022-07-15
clang-format 13.0.0: LambdaBodyIndentation: OuterScope closing brace & parenthesis too indented 4 2022-01-15 2022-07-25
Show files in remote server via scp 5 2021-08-21 2022-09-29
InputNumber input event returns wrong value 0 2021-09-06 2022-08-21
[minor] clean branch iasecc_cpx 1 2021-05-01 2022-09-26
LibIndy:android build instructions 0 2021-10-01 2022-09-22
DemoBuildCs Crashes After “Select Folder” Clicked 3 2020-06-12 2022-09-23
SortingItemsWinUI3Demo Exception and Strange Windowing Behavior 0 2021-02-18 2022-09-23
i am integrating ffmpeg-kit in react native but every time its fail fail if anyone have example simple then please give me please 1 2021-11-27 2022-09-07
it can not detect include file automatically. 0 2019-09-04 2022-07-20
Change ChainSpec::from_genesis for Rialto and Millau chains to reflect the chain names. 0 2021-07-30 2022-08-22
Offer link preload opt-in 0 2022-02-23 2022-09-22
visual studio code is flashing 2 2021-11-16 2022-08-23
web3.js changes writability of accounts 2 2021-12-20 2022-07-22
Add Support for profiling in Tabular form 1 2019-06-11 2022-09-09
[BUG]v8.0.0-rc.1,使用createDecorator创建自定义装饰器,如果用在多次继承的类里,会导致混乱 5 2021-07-19 2022-09-28
class"" suggestions not working on VSCode Vue project 6 2021-07-10 2022-09-29