Title: Projected content checked, even if component is detached · Issue #21324 · angular/angular · GitHub
Open Graph Title: Projected content checked, even if component is detached · Issue #21324 · angular/angular
X Title: Projected content checked, even if component is detached · Issue #21324 · angular/angular
Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x ] Feature request [ ] Documentation issue or request [ ] Support request => Please do not submit support request her...
Open Graph Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x ] Feature request [ ] Documentation issue or request [ ] Support request => ...
X Description: I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x ] Feature request [ ] Documentation issue or request [ ] Support request...
Opengraph URL: https://github.com/angular/angular/issues/21324
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Projected content checked, even if component is detached","articleBody":"\u003c!--\r\nPLEASE HELP US PROCESS GITHUB ISSUES FASTER BY PROVIDING THE FOLLOWING INFORMATION.\r\n\r\nISSUES MISSING IMPORTANT INFORMATION MAY BE CLOSED WITHOUT INVESTIGATION.\r\n-->\r\n\r\n## I'm submitting a...\r\n\u003c!-- Check one of the following options with \"x\" -->\r\n\u003cpre>\u003ccode>\r\n[ ] Regression (a behavior that used to work and stopped working in a new release)\r\n[ ] Bug report \u003c!-- Please search GitHub for a similar issue or PR before submitting -->\r\n[x ] Feature request\r\n[ ] Documentation issue or request\r\n[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question\r\n\u003c/code>\u003c/pre>\r\n\r\n## Current behavior\r\n\r\n\u003c!-- Describe how the issue manifests. -->\r\nI created a generic table component, which allows to define columns via templates. My problem is:\r\nChange-detection is triggered, even if the component is detached due to content projection (see the discussion at https://stackoverflow.com/questions/48096659/interpolation-executed-even-if-changedetectorref-is-detached/48108250). \r\n\r\nA simplified version of the problem:\r\n\r\n- the root-component has a H1-Element with a click-handler (the root-component uses ChangeDetectionStrategy.Default)\r\n- below the clickable H1-Element is another component, which renders some rows. A template is specified, which defines how to render a row (and a function-call is used to get the value to render. I know, for this case a function doesn't make sense but it illustrates the problem)\r\n- the component, which renders the row, is detached from change-Detection\r\n- if I click on the h1-Element, the function is invoked again for each row in the component, although it is detached from change-detection.\r\n\r\n**Code (from an angular-CLI project):**\r\n\r\nThe code can be found at https://stackblitz.com/edit/angular-zcyf9w?file=app%2Fapp.component.ts\r\n\r\n### app.component.html\r\n```\r\n\u003ch1 (click)=\"onClick()\" style=\"cursor:pointer\">Klick me\u003c/h1>\r\n\r\n\u003capp-rows [rows]=\"rows\">\r\n \u003cng-template let-row=\"rowData\">\r\n {{getName(row)}}\r\n \u003c/ng-template>\r\n\u003c/app-rows>\r\n```\r\n### app.component.ts\r\n```\r\n@Component({\r\n selector: 'app-root',\r\n templateUrl: './app.component.html'\r\n})\r\nexport class AppComponent {\r\n\r\n rows: any[];\r\n\r\n constructor(private changeDetectorRef: ChangeDetectorRef, private renderer: Renderer2) {\r\n this.rows = this.createRows(100);\r\n }\r\n\r\n getName(row:any): string {\r\n console.log(\"getName called\")\r\n return row.name;\r\n }\r\n\r\n onClick() {\r\n console.log(\"clicked\");\r\n }\r\n\r\n private createRows(count:number): any[] {\r\n let rows: any[] = [];\r\n\r\n for (let i=0; i \u003c count; i++) {\r\n rows.push({\r\n name: \"TEST \" + (i+1)\r\n })\r\n }\r\n return rows;\r\n }\r\n}\r\n```\r\n\r\n### rows.component.html\r\n```\r\n\u003cdiv *ngFor=\"let row of rows\">\r\n \u003cng-container *ngTemplateOutlet=\"template; context: {'rowData': row}\">\r\n \u003c/ng-container>\r\n\u003c/div>\r\n```\r\n\r\n### rows.component.ts\r\n```\r\n@Component({\r\n selector: 'app-rows',\r\n templateUrl: './rows.component.html'\r\n})\r\nexport class RowsComponent {\r\n\r\n @Input()\r\n rows: any[];\r\n\r\n @ContentChild(TemplateRef)\r\n template: TemplateRef\u003cany>;\r\n\r\n constructor(private changeDetector: ChangeDetectorRef) {\r\n }\r\n\r\n ngAfterViewInit() {\r\n this.changeDetector.detach();\r\n }\r\n}\r\n\r\n```\r\n\r\nI understand, that the change-detection is triggered, because of content-projection. \r\n\r\n## Expected behavior\r\n\r\nThere should be a way to prevent check of projected views. The function `markParentViewsForCheckProjectedViews ` in `utils.ts` updates the state of **ALL** views in the hierarchy with `ViewState.CheckProjectedViews`.\r\n\r\nProposed solution:\r\n\r\n- introduce a new ViewState: `ProjectedViewsDetached`\r\n- Extend `ChangeDetectorRef `with new methods `detachProjectedViews()` and `reattachProjectedViews()` (which sets/removes the view-state `ProjectedViewsDetached`)\r\n- Change the function `markParentViewsForCheckProjectedViews` to something like this:\r\n\r\n```\r\nexport function markParentViewsForCheckProjectedViews(view: ViewData, endView: ViewData) {\r\n let currView: ViewData|null = view;\r\n while (currView && currView !== endView) {\r\n if (currView.state & ViewState.ProjectedViewsDetached) {\r\n\t\tcurrView.state &= ~ViewState.CheckProjectedViews;\r\n\t} else {\r\n\t\tcurrView.state |= ViewState.CheckProjectedViews;\r\n\t}\r\n currView = currView.viewContainerParent || currView.parent;\r\n }\r\n}\r\n```\r\n\r\nThis way I could change the ngAfterViewInit()-lifecycle method of rows.component.ts to \r\n\r\n```\r\nngAfterViewInit() {\r\n this.changeDetector.detach();\r\n this.changeDetector.detachProjectedViews();\r\n}\r\n```\r\n\r\nThis solution should fix this type of problems and does not change the behaviour of applications, which do not use the method `detachProjectedViews()` of the `ChangeDetectorRef`.\r\n\r\nA simpler solution might be just to change markParentViewsForCheckProjectedViews without introducing a new ViewState:\r\n```\r\nexport function markParentViewsForCheckProjectedViews(view: ViewData, endView: ViewData) {\r\n let currView: ViewData|null = view;\r\n while (currView && currView !== endView) {\r\n if (currView.state & ViewState.Attached) {\r\n\t\tcurrView.state |= ViewState.CheckProjectedViews;\r\n\t} else {\r\n\t\tcurrView.state &= ~ViewState.CheckProjectedViews;\r\n\t}\r\n currView = currView.viewContainerParent || currView.parent;\r\n }\r\n}\r\n```\r\n\r\nBut this solution might create side-effects on existing applications.\r\n\r\n## Minimal reproduction of the problem with instructions\r\n\r\nSee the running example at https://stackblitz.com/edit/angular-zcyf9w?file=app%2Fapp.component.ts\r\n\r\n\u003c!--\r\nFor bug reports please provide the *STEPS TO REPRODUCE* and if possible a *MINIMAL DEMO* of the problem via\r\nhttps://plnkr.co or similar (you can use this template as a starting point: http://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5).\r\n-->\r\n\r\n## What is the motivation / use case for changing the behavior?\r\n\r\nI need tables with a lot of rows ( > 1000) to perform well. \r\n\r\nI'm aware of solutions like virtual scrolling but that introduces other issues (e.g.: dynamic height of table-rows, which makes it nearly impossible to calculate a correct height of the entire table).\r\n\r\n\r\n## Environment\r\n\r\n\u003cpre>\u003ccode>\r\nAngular version: 5.1.3\r\n\r\n\r\nBrowser:\r\n- [x] Chrome (desktop) version XX\r\n- [ ] Chrome (Android) version XX\r\n- [ ] Chrome (iOS) version XX\r\n- [x] Firefox version XX\r\n- [ ] Safari (desktop) version XX\r\n- [ ] Safari (iOS) version XX\r\n- [ ] IE version XX\r\n- [ ] Edge version XX\r\n \r\nFor Tooling issues:\r\n- Node version: 6.11.2\r\n- Platform: Windows\r\n\r\n \r\n ","author":{"url":"https://github.com/rehbein-dieter","@type":"Person","name":"rehbein-dieter"},"datePublished":"2018-01-05T07:59:19Z","url":"https://github.com/angular/angular/issues/21324"}
| 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:4e5bcf1b-85a3-c935-adf0-32f47bce601b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C330:1201C:DA90A0:12413D8:6B00FFB9 |
| html-safe-nonce | 76253c8317d41ecba6bb59de8261e07c671c7f85a1b74bffd3d35018a8454d79 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzMwOjEyMDFDOkRBOTBBMDoxMjQxM0Q4OjZCMDBGRkI5IiwidmlzaXRvcl9pZCI6IjUxMzU1MTk2ODY4MjE4MDUzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3563dab87c734413c021d33afbd124ef7ed90920e7ece416476e5b8019d17aca |
| hovercard-subject-tag | issue:286217687 |
| 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/21324/issue_layout |
| twitter:image | https://opengraph.githubassets.com/06ec7e2da2cfea803bfd695450a803fe7763e874a507c47acd6b4ff0b5e9fafb/angular/angular/issues/21324 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/06ec7e2da2cfea803bfd695450a803fe7763e874a507c47acd6b4ff0b5e9fafb/angular/angular/issues/21324 |
| og:image:alt | I'm submitting a... [ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [x ] Feature request [ ] Documentation issue or request [ ] Support request => ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rehbein-dieter |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width