Title: Implement @ObserveChildren or similar API · Issue #5467 · angular/angular · GitHub
Open Graph Title: Implement @ObserveChildren or similar API · Issue #5467 · angular/angular
X Title: Implement @ObserveChildren or similar API · Issue #5467 · angular/angular
Description: Previously discussed with @vsavkin and @jeffbcross Background - programmatically listening to events propagated by children is difficult currently. Consider: You have a
Open Graph Description: Previously discussed with @vsavkin and @jeffbcross Background - programmatically listening to events propagated by children is difficult currently. Consider: You have a
X Description: Previously discussed with @vsavkin and @jeffbcross Background - programmatically listening to events propagated by children is difficult currently. Consider: You have a <accordion> component ...
Opengraph URL: https://github.com/angular/angular/issues/5467
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Implement @ObserveChildren or similar API","articleBody":"Previously discussed with @vsavkin and @jeffbcross \n\nBackground - programmatically listening to events propagated by children is difficult currently. \n\nConsider: \nYou have a `\u003caccordion>` component and n `\u003caccordion-item>` components:\n\n```\n\u003caccordion>\n \u003caccordion-item>\u003c/accordion-item>\n \u003caccordion-item>\u003c/accordion-item>\n \u003caccordion-item>\u003c/accordion-item>\n\u003c/accordion>\n```\n\nAn accordion-item component might emit a `select` event/output, used to set it as the currently active item.\n\nCurrently the best way to achieve this is the accordion-item component would inject the parent accordion, and have the accordion-item register itself with the parent, and call a method to close the others\n\n``` typescript\n@Component({selector: 'accordion' })\nclass Accordion {\n _items: []AccordionItem = [];\n register(item: AccordionItem){\n this._items.push(item);\n }\n toggleActive(selectedItem:AccordionItem){\n this._items.forEach(item => {\n if(item !== selectedItem){\n item.active = false;\n }\n });\n selectedItem.active = true;\n }\n}\n\n@Component({selector: 'accordion-item' , inputs: ['active'] })\nclass AccordionItem {\n active: boolean;\n constructor(private accordion: Accordion){\n accordion.register(this);\n }\n setActive(){\n this.accordion.toggleActive(this);\n }\n}\n```\n\nIdeally, a child component would simply expose a 'select' event:\n\n``` typescript\n@Component({selector: 'accordion-item' })\nclass AccordionItem {\n @Input() active: boolean;\n @Output selected:EventEmitter\u003cAccordionItem> = new EventEmitter();\n active: boolean;\n setActive(){\n this.selected.emit(this);\n }\n}\n```\n\nThe parent accordion can currently easily get ahold of the children via `@ViewChildren()` (or whatever) but there is a not a clean way to listen to events emitted from the children. \n\nProposed Syntax:\n\nImplement a `@ObserveChildren` decorator (or similar) that would enable Observation of events emitted by child components:\n\n``` typescript\n@Component({selector: 'accordion' })\nclass Accordion {\n //specify type and desired output to Observe.\n @ObserveChildren(AccordionItem, 'select') childSelections: Observable\u003cAccordionItem>\n @ViewChildren(AccordionItem) children: Observable\u003cAccordionItem>\n afterViewInit(){\n this.updates = Observable\n .combineLatest(this.children, this.childSelections, (children, selected) => ({children, selected}))\n .do(({children, selected) => children.map(child => { //update state }))\n .subscribe();\n }\n onDestroy(){\n this.updates.unsubscribe();\n }\n}\n```\n\ncc @pkozlowski-opensource @Foxandxss \n","author":{"url":"https://github.com/robwormald","@type":"Person","name":"robwormald"},"datePublished":"2015-11-24T22:01:56Z","url":"https://github.com/angular/angular/issues/5467"}
| 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:4b0330de-3959-9910-8c79-6bcd745082c2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D036:117C62:194399E:21A92A5:6B0152AA |
| html-safe-nonce | 8274e9d917f632a4d54be15e98577c21bb334cc0ed884a1021aa9c3460708fa9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDM2OjExN0M2MjoxOTQzOTlFOjIxQTkyQTU6NkIwMTUyQUEiLCJ2aXNpdG9yX2lkIjoiMjA5NjUxNTIwNDE3NDE0MDA3NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 33e7a23f532a23b1056e4fca6f0c48534fc9ccfb4bccd7dbd3260d7dbb96cefa |
| hovercard-subject-tag | issue:118712587 |
| 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/5467/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9deb74e2593dc67b4bdbc18af810ee444aa1a01a33132b26b293e88da679c126/angular/angular/issues/5467 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9deb74e2593dc67b4bdbc18af810ee444aa1a01a33132b26b293e88da679c126/angular/angular/issues/5467 |
| og:image:alt | Previously discussed with @vsavkin and @jeffbcross Background - programmatically listening to events propagated by children is difficult currently. Consider: You have a |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | robwormald |
| 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