Title: Template of an OnPush component is no longer updated after a signal change in some cases · Issue #50320 · angular/angular · GitHub
Open Graph Title: Template of an OnPush component is no longer updated after a signal change in some cases · Issue #50320 · angular/angular
X Title: Template of an OnPush component is no longer updated after a signal change in some cases · Issue #50320 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? No Description Go to this stackblitz Click on the Toggle state button. item is visible appears Click again on the Toggle state button. item is visible disa...
Open Graph Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? No Description Go to this stackblitz Click on the Toggle state button. item is visible appears Click again on the T...
X Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? No Description Go to this stackblitz Click on the Toggle state button. item is visible appears Click again on the T...
Opengraph URL: https://github.com/angular/angular/issues/50320
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Template of an OnPush component is no longer updated after a signal change in some cases","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\ncore\n\n### Is this a regression?\n\nNo\n\n### Description\n\nGo to [this stackblitz](https://stackblitz.com/edit/angular-nltu6v?file=src/main.ts)\r\nClick on the `Toggle state` button.\r\n`item is visible` appears\r\nClick again on the `Toggle state` button.\r\n`item is visible` disappears\r\nClick again on the `Toggle state` button.\r\nNothing happens! I would expect the `item is visible` element to appear again.\r\n\r\nHere is the code of the stackblitz:\r\n\r\n```ts\r\nimport 'zone.js/dist/zone';\r\nimport {\r\n ChangeDetectionStrategy,\r\n Component,\r\n Directive,\r\n effect,\r\n signal,\r\n Input,\r\n OnChanges,\r\n OnDestroy,\r\n SimpleChanges,\r\n computed,\r\n} from '@angular/core';\r\nimport { CommonModule } from '@angular/common';\r\nimport { bootstrapApplication } from '@angular/platform-browser';\r\n\r\ninterface State {\r\n isInDom: boolean;\r\n shouldBeInDom: boolean;\r\n}\r\n\r\nconst createModel = () => {\r\n const state = signal\u003cState>({\r\n isInDom: false,\r\n shouldBeInDom: false,\r\n });\r\n const updateState = (changes: Partial\u003cState>) =>\r\n state.update((value) => ({\r\n ...value,\r\n ...changes,\r\n }));\r\n const toggle = () => updateState({ shouldBeInDom: !state().shouldBeInDom });\r\n const setDomState = (isInDom: boolean) => updateState({ isInDom });\r\n return { state, toggle, setDomState };\r\n};\r\n\r\n@Directive({\r\n standalone: true,\r\n selector: '[myDirective]',\r\n})\r\nexport class MyDirective implements OnChanges, OnDestroy {\r\n @Input({ required: true })\r\n myDirective!: ReturnType\u003ctypeof createModel>;\r\n\r\n async ngOnChanges(changes: SimpleChanges) {\r\n // Comment the following line to have the issue on display:\r\n await 0;\r\n this.myDirective.setDomState(true);\r\n }\r\n\r\n async ngOnDestroy() {\r\n // Uncomment the following line as a work-around to fix the issue:\r\n // await 0;\r\n this.myDirective.setDomState(false);\r\n }\r\n}\r\n\r\n@Component({\r\n selector: 'my-component',\r\n standalone: true,\r\n // Comment the following line as a work-around to fix the issue:\r\n changeDetection: ChangeDetectionStrategy.OnPush,\r\n imports: [CommonModule, MyDirective],\r\n template: `\r\n \u003cdiv *ngIf=\"state().shouldBeInDom\" [myDirective]=\"model\">item is visible\u003c/div>\r\n `,\r\n})\r\nexport class MyComponent {\r\n model = createModel();\r\n // Uncomment the following line as a work-around to fix the issue:\r\n // state = this.model.state;\r\n state = computed(() => this.model.state());\r\n\r\n constructor() {\r\n // Uncomment the following line as a work-around to fix the issue:\r\n // effect(() => console.log(this.state()));\r\n }\r\n}\r\n\r\n@Component({\r\n selector: 'my-app',\r\n standalone: true,\r\n imports: [CommonModule, MyComponent],\r\n template: `\r\n \u003cbutton (click)=\"c.model.toggle()\">Toggle state\u003c/button>\r\n \u003cmy-component #c>\u003c/my-component>\r\n `,\r\n})\r\nexport class App {}\r\n\r\nbootstrapApplication(App);\r\n```\n\n### Please provide a link to a minimal reproduction of the bug\n\nhttps://stackblitz.com/edit/angular-nltu6v?file=src/main.ts\n\n### Please provide the exception or error you saw\n\n```true\nThere is no exception, no explanation.\n```\n\n\n### Please provide the environment you discovered this bug in (run `ng version`)\n\n```true\nAngular CLI: 16.0.1\r\nNode: 18.16.0\r\nPackage Manager: npm 9.5.1\r\nOS: linux x64\r\n\r\nAngular: 16.0.1\r\n... animations, cli, common, compiler, compiler-cli, core, forms\r\n... platform-browser, platform-browser-dynamic, router\r\n\r\nPackage Version\r\n---------------------------------------------------------\r\n@angular-devkit/architect 0.1600.1\r\n@angular-devkit/build-angular 16.0.1\r\n@angular-devkit/core 16.0.1\r\n@angular-devkit/schematics 16.0.1\r\n@schematics/angular 16.0.1\r\nng-packagr 16.0.1\r\nrxjs 7.8.1\r\ntypescript 5.0.4\n```\n\n\n### Anything else?\n\nWhat triggers the bug seems to be the fact that the signal is synchronously changed either from `ngOnChanges` or from `ngOnDestroy` in the directive. If this is done asynchronously, the problem does not happen.\r\n\r\nThis makes me think about the famous `ExpressionChangedAfterItHasBeenCheckedError` issue (even if there is no exception displayed in this case), the idea is the same: there is an issue when updating data at some specific times.\r\n\r\nAs mentioned in the comments of the code, if any of the following modifications is done (alone), the bug does not happen:\r\n- if `state` is a direct reference to `this.model.state` in `MyComponent` instead of being a `computed`\r\n- if an effect that reads `this.state()` is added in the constructor of `MyComponent`\r\n- if `MyComponent` is not `OnPush`\r\n","author":{"url":"https://github.com/divdavem","@type":"Person","name":"divdavem"},"datePublished":"2023-05-16T11:17:31Z","url":"https://github.com/angular/angular/issues/50320"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f3dff4da-76db-d678-0c3c-882e1ce76a0a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9318:26ECEB:165CA7E:1C8C833:6B01228C |
| html-safe-nonce | 52b56759733cd5c9b7d55a6cc53208ff7956d4df28e354617353109460bbe0f8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MzE4OjI2RUNFQjoxNjVDQTdFOjFDOEM4MzM6NkIwMTIyOEMiLCJ2aXNpdG9yX2lkIjoiNDQ0MTc1MzcxNTAxMTAzNTc4OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f1c878e7e263a7cc72085eee3f4975edf9b9bc096672b08b94862966a1f1d0ca |
| hovercard-subject-tag | issue:1711809600 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/50320/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7cebd9ae191599261b63b5178beca184ca17f115b00f249894334ab195b6ad07/angular/angular/issues/50320 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7cebd9ae191599261b63b5178beca184ca17f115b00f249894334ab195b6ad07/angular/angular/issues/50320 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? core Is this a regression? No Description Go to this stackblitz Click on the Toggle state button. item is visible appears Click again on the T... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | divdavem |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width