Title: Components that contain ViewChild need change detection after view init · Issue #5870 · angular/angular · GitHub
Open Graph Title: Components that contain ViewChild need change detection after view init · Issue #5870 · angular/angular
X Title: Components that contain ViewChild need change detection after view init · Issue #5870 · angular/angular
Description: The following example shows a Component that has a reference to a child directive using a ViewChild query. If the input changes and causes NgFor to create new components, those will execute all view bindings before the child directive is...
Open Graph Description: The following example shows a Component that has a reference to a child directive using a ViewChild query. If the input changes and causes NgFor to create new components, those will execute all vie...
X Description: The following example shows a Component that has a reference to a child directive using a ViewChild query. If the input changes and causes NgFor to create new components, those will execute all vie...
Opengraph URL: https://github.com/angular/angular/issues/5870
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Components that contain ViewChild need change detection after view init","articleBody":"The following example shows a Component that has a reference to a child directive using a `ViewChild` query. If the input changes and causes `NgFor` to create new components, those will execute all view bindings before the child directive is obtained in the component using the `ViewChild` annotation. After the view child is set and after the `ngAfterViewInit` lifecycle hook was called, there will be no additional change detection as the component thinks he's clean. However, if the component has a reference to a view child and this reference is used within the components view, the component should be dirty after the `ngAfterViewInit` lifecycle hook.\n\nThe following example should show all even numbers in blue and all odd numbers in red. Due to the change detection issue, after clicking the \"Cause Change\" button, there is an inconsistency between the view and the model.\n\nThe example can be used as follows to reproduce the issue:\n1. Click the \"Cause Change\" button to change the input array to the `NgFor` directive. `NgFor` will detect the third and fourth element to be new and creates them. The view bindings that access the child directive are execute before the ViewChild was initialized. The component stays checked, even if the ViewChild is referenced within the components view.\n2. Click the \"Cause Change\" button again, where no additional components get created (the list differ does its job), but the view bindings execute again and the issue is gone.\n\nhttp://plnkr.co/edit/zSiHmyzXcm6Yq1jHJgMn?p=preview\n\n``` javascript\nimport {Component, Directive, Input, ViewChild, NgFor} from 'angular2/angular2';\n\n@Directive({\n selector: '[number]'\n})\nexport class NumberDirective {\n @Input() number;\n\n isEven() {\n return this.number % 2 === 0;\n }\n}\n\n@Component({\n selector: 'number-component'\n template: `\n \u003cdiv [number]=\"number\" \n [style.color]=\"getColor()\">{{number}}\u003c/div>\n `,\n directives: [NumberDirective]\n})\nexport class NumberComponent {\n @ViewChild(NumberDirective) numberDirective;\n @Input() number;\n\n constructor() {\n console.log('Constructed');\n }\n\n ngOnChange() {\n console.log('on change', this.numberDirective);\n }\n\n ngAfterViewInit() {\n console.log('after view init', this.numberDirective);\n }\n\n getColor() {\n console.log('getColor', this.numberDirective);\n return this.numberDirective && this.numberDirective.isEven() ? 'blue' : 'red';\n }\n}\n\n@Component({\n selector: 'app'\n template: `\n \u003cnumber-component *ngFor=\"#number of numbers\" \n [number]=\"number\">\u003c/number-component>\n \u003cbutton (click)=\"change()\">Cause Change\u003c/button>\n `,\n directives: [NumberComponent, NgFor]\n})\nexport class App {\n numbers = [1, 2];\n\n change() {\n this.numbers = [1, 2, 3, 4];\n }\n}\n```\n","author":{"url":"https://github.com/gionkunz","@type":"Person","name":"gionkunz"},"datePublished":"2015-12-13T18:51:45Z","url":"https://github.com/angular/angular/issues/5870"}
| 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:bc4b3387-9416-cf3c-9316-e580e8fb3ae8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B0DC:95C88:1A276F4:228B713:6B014797 |
| html-safe-nonce | b7c14f69132ebc23216b1ad4201692c4a1f9e555e41df61843f16298b8f353d7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMERDOjk1Qzg4OjFBMjc2RjQ6MjI4QjcxMzo2QjAxNDc5NyIsInZpc2l0b3JfaWQiOiI3NDE3Njg5NDkwMTU3ODE1NzAzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2cc824ea413bd8b1f38ae2e125e602e0828a6b9a96eba47f768f216d8dddf084 |
| hovercard-subject-tag | issue:121936404 |
| 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/5870/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d9c2afa6e3c3c2537b329a1aea3c1315bfe7b5f1889404adc15f64d7ab0a3811/angular/angular/issues/5870 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d9c2afa6e3c3c2537b329a1aea3c1315bfe7b5f1889404adc15f64d7ab0a3811/angular/angular/issues/5870 |
| og:image:alt | The following example shows a Component that has a reference to a child directive using a ViewChild query. If the input changes and causes NgFor to create new components, those will execute all vie... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gionkunz |
| 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