{"id":21307807,"url":"https://github.com/jdegand/bug-cd","last_synced_at":"2025-03-15T20:13:31.023Z","repository":{"id":198264693,"uuid":"694897146","full_name":"jdegand/bug-cd","owner":"jdegand","description":"Angular Challenges #32 Bug Change Detection","archived":false,"fork":false,"pushed_at":"2023-10-04T18:15:45.000Z","size":117,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T09:13:37.524Z","etag":null,"topics":["angular","angular-challenges","change-detection"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jdegand.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-21T23:22:55.000Z","updated_at":"2023-09-21T23:27:21.000Z","dependencies_parsed_at":"2023-11-14T03:43:07.808Z","dependency_job_id":null,"html_url":"https://github.com/jdegand/bug-cd","commit_stats":{"total_commits":2,"total_committers":1,"mean_commits":2.0,"dds":0.0,"last_synced_commit":"104a4bb3cea6985a5bcb26c92cfd12aeb727b016"},"previous_names":["jdegand/bug-cd"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdegand%2Fbug-cd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdegand%2Fbug-cd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdegand%2Fbug-cd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdegand%2Fbug-cd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdegand","download_url":"https://codeload.github.com/jdegand/bug-cd/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243785089,"owners_count":20347409,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","angular-challenges","change-detection"],"created_at":"2024-11-21T16:34:28.528Z","updated_at":"2025-03-15T20:13:30.964Z","avatar_url":"https://github.com/jdegand.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bug Cd\n\n[Angular Challenges](https://github.com/tomalaforge/angular-challenges) #32 Bug Change Detection Solution\n\n## Built With\n\n- [Angular](https://angular.io)\n- [Angular CLI](https://github.com/angular/angular-cli) version 16.2.2.\n\n## Directions\n\nIn this small application, we have a navigation menu to route our application to either `barComponent` or `FooComponent`. However our application is not loading and no errors are displayed inside the console.\n\n### Statement\n\nThe goal of the challenge is to debug this application and make it work.\n\n#### Hints\n\n\u003cdetails\u003e\n  \u003csummary\u003eHint 1\u003c/summary\u003e\n  \n  If you comment out `routerLinkActive=\"isSelected\"` inside `NavigationComponent`: the application loads correctly.\n\u003c/details\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eHint 2\u003c/summary\u003e\n\nIf you open the [`RouterLinkActive` source code](https://github.com/angular/angular/blob/main/packages/router/src/directives/router_link_active.ts) and go to **line 196**, Angular is calling `this.cdr.markForCheck` inside a microTask which triggers a new CD cycle. If you comment out this line, the application loads again, however the bug is not inside the Angular Framework. \n\n\u003c/details\u003e\n\n## Thoughts\n\n- App is slow and you can't refresh it. \n- I didn't like the order of the routes so I changed them.  Although it doesn't really matter in this case, I always prefer to have empty routes at the bottom (with a wildcard route as the final route).  \n- Having getMenu function calls inside the template is a problem.   \n- Use a pipe ?  ngOnChanges ?\n- I initially leaned towards ngOnChanges as many challenges I have already used pipes.    \n- The fake backend response (string) actually makes this harder to fix with ngOnChanges.\n- There will be typing issues if you just pass a string to menus.  Once you fix that, then you call the getMenu function inside of ngOnChanges.  Then you have to worry about return types and issues in the template.  \n- I think it would be preferrable to have the @Input just be the object itself.  But that wouldn't be enough as you would need to pass the Input value to a function that adds the prop value inside the ngOnChanges function.   \n- Ultimately, I think ngOnChanges would require too much modification to the existing code to be a realistic solution.  \n- I found some great articles and there is an easy fix.  Use a memo function and you can keep the function calls in the template.  See [this article](https://itnext.io/its-ok-to-use-function-calls-in-angular-templates-ffdd12b0789e) for the memo function and explanation.\n\n## Useful Resources\n\n- [Angular Docs](https://angular.io/api/router/RouterLinkActive#description) - RouterLinkActive\n- [Stack Overflow](https://stackoverflow.com/questions/33520043/how-to-detect-a-route-change-in-angular) - how to detect a route change in angular\n- [Stack Overflow](https://stackoverflow.com/questions/57916707/angular-8-ui-observable-not-triggering-change-detection-changedetectionstrateg) - angular 8 observable not triggering change detection\n- [Stack Overflow](https://stackoverflow.com/questions/41364386/whats-the-difference-between-markforcheck-and-detectchanges) - whats the difference between markForCheck and detectChanges ?\n- [Medium](https://medium.com/@andre.schouten_ff/whats-the-difference-between-markforcheck-and-detectchanges-in-angular-fff4e5f54d34) - difference between markForCheck and detectChanges\n- [Medium](https://medium.com/showpad-engineering/why-you-should-never-use-function-calls-in-angular-template-expressions-e1a50f9c0496#:~:text=In%20this%20article%2C%20we%20learned,calls%20in%20Angular%20template%20expressions.) - why you should never use function calls in angular template expressions\n- [Blog](https://itnext.io/its-ok-to-use-function-calls-in-angular-templates-ffdd12b0789e) - its ok to use function calls in angular templates\n- [Dev.to](https://dev.to/nickraphael/ngonchanges-best-practice-always-use-simplechanges-always-1feg) - ngOnChanges best practice always use simpleChanges","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdegand%2Fbug-cd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdegand%2Fbug-cd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdegand%2Fbug-cd/lists"}