[{"id":"angularadvanced-001","courseId":"angular-advanced","domain":"components-and-templates","topic":"Lifecycle hook execution order","prompt":"You need to read a view query's result once, after the component's template children are initialized. Which lifecycle hook fits?","hint":"Some hooks run on every check; pick the one that runs a single time.","explain":"Lifecycle hooks run at fixed points in a component's life. Some run once after a set of children initializes, while others run on every check, which costs performance.","source":"https://angular.dev/guide/components/lifecycle","interaction":{"type":"single"},"options":[{"id":"a","text":"ngAfterViewInit","why":"It runs once after all children in the component's template have been initialized, which is when view query results are ready to read."},{"id":"b","text":"ngAfterViewChecked","whyNot":"It runs every time the view is checked, and the guide says to avoid it, so it is no place for a one-time read."},{"id":"c","text":"ngAfterContentInit","whyNot":"It runs once after nested content children initialize, so it is for content queries, not view queries."},{"id":"d","text":"ngDoCheck","whyNot":"It runs before every time Angular checks the template for changes; it is for manual change checks, not view query results."}],"answer":"a","explanation":"ngAfterViewInit runs once after the component's view children are initialized and is the hook for reading view query results.","concept":"ngAfterViewInit runs once after the component's view children are initialized and is the hook for reading view query results.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Hooks that run on every check cost performance; reach for the one-time hook first."]},{"id":"angularadvanced-002","courseId":"angular-advanced","domain":"components-and-templates","topic":"afterNextRender for DOM work","prompt":"A render callback from afterNextRender splits its DOM work into phases. Put the four phases in the order Angular runs them.","hint":"Layout writes should land before the measurements that depend on them.","explain":"Phase-split render callbacks help you order DOM work so layout is not recalculated again and again. Each phase has a defined job, and you pick the phase that fits each step.","source":"https://angular.dev/guide/components/lifecycle","interaction":{"type":"sort","items":[{"text":"earlyRead","why":"Runs first, reading only the layout values that later calculations strictly need."},{"text":"write","why":"Runs after the early reads, so layout-affecting styles change before anything measures them."},{"text":"mixedReadWrite","why":"Runs after the explicit write work; it is the default phase, and the guide says to avoid it when possible."},{"text":"read","why":"Runs last, reading geometric properties after all the writes have occurred."}]},"options":[],"answer":"o:abcd","explanation":"afterNextRender and afterEveryRender run their callbacks in four phases: earlyRead, write, mixedReadWrite, then read.","concept":"afterNextRender and afterEveryRender run their callbacks in four phases: earlyRead, write, mixedReadWrite, then read.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Batch DOM writes before DOM reads to avoid layout thrashing."]},{"id":"angularadvanced-003","courseId":"angular-advanced","domain":"components-and-templates","topic":"ExpressionChanged error from ngAfter hooks","prompt":"Inside ngAfterViewInit, assigning a new value to a property that the template binds is safe, and Angular updates the screen without any error.","hint":"Ask whether Angular is still allowed to change the view after this hook runs.","explain":"Angular checks a component's view and then runs the after-check hooks. Reading state there is allowed; the detail to know is what happens when state changes during that window.","source":"https://angular.dev/guide/components/lifecycle","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"This is a common belief, but changing state in this hook is what triggers ExpressionChangedAfterItHasBeenCheckedError."},{"id":"b","text":"False","why":"Attempting to change any state in ngAfterViewInit results in ExpressionChangedAfterItHasBeenCheckedError, so the statement is false."}],"answer":"b","explanation":"Changing state in ngAfterViewInit or ngAfterContentInit (and their Checked variants) results in ExpressionChangedAfterItHasBeenCheckedError.","concept":"Changing state in ngAfterViewInit or ngAfterContentInit (and their Checked variants) results in ExpressionChangedAfterItHasBeenCheckedError.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Changing bound state in an after-check hook raises ExpressionChangedAfterItHasBeenCheckedError; move the change earlier."]},{"id":"angularadvanced-004","courseId":"angular-advanced","domain":"components-and-templates","topic":"host property over decorators","prompt":"You are writing a new component that must set role='slider' on its host element and react to keydown. Which approach does the guide recommend?","hint":"Consider how many ways Angular offers to bind to the host element, and what the guide says about each.","explain":"A component can bind attributes, styles, and events to its own host element, the tag it is used in. Angular offers several syntaxes for these bindings, and the guide describes each one's status differently.","source":"https://angular.dev/guide/components/host-elements","interaction":{"type":"single"},"options":[{"id":"a","text":"A host property in the @Component decorator, such as '(keydown)': 'onKey($event)'","why":"The guide says to always prefer the host property; the decorator forms exist only for backwards compatibility."},{"id":"b","text":"@HostBinding('attr.role') and @HostListener('keydown') on class members","whyNot":"They work, but the guide says they exist exclusively for backwards compatibility, so new code should not start with them."},{"id":"c","text":"A role attribute on an inner div in the template, since the host element cannot take bindings","whyNot":"The host element can take bindings, and they behave the same as bindings inside the template."},{"id":"d","text":"Calling setAttribute and addEventListener in the constructor","whyNot":"Manual DOM calls can run, but they are not the approach the guide recommends for host bindings."}],"answer":"a","explanation":"The host property is the preferred way to bind the host element; @HostBinding and @HostListener exist only for backwards compatibility.","concept":"The host property is the preferred way to bind the host element; @HostBinding and @HostListener exist only for backwards compatibility.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["For new code, put host bindings and listeners in the host property of @Component."]},{"id":"angularadvanced-005","courseId":"angular-advanced","domain":"components-and-templates","topic":"Host binding collisions","prompt":"A component's host sets the static role 'presentation' and the dynamic [id] binding to 'id'. A user writes the element below. Which TWO outcomes does Angular produce?","hint":"Check whether each colliding value is static or dynamic before deciding which one wins.","explain":"A component's host and the template using it can both set the same attribute or property. Angular resolves the clash using fixed rules based on whether each value is static or dynamic.","source":"https://angular.dev/guide/components/host-elements","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"The role is 'group', because the instance binding wins when both values are static.","why":"Both role values are static, so the instance binding wins and the element gets role group."},{"id":"b","text":"The id comes from the component's host binding, since both id bindings are dynamic.","why":"When both values are dynamic, the component's host binding wins over the instance binding."},{"id":"c","text":"The role is 'presentation', because host bindings always override the template.","whyNot":"Host values do not always win; with two static values the instance binding wins."},{"id":"d","text":"The id is otherId, because the instance binding always overrides the host.","whyNot":"The instance wins over a static host value only; with two dynamic values the host binding wins."},{"id":"e","text":"Angular merges the two roles into one value, 'group presentation'.","whyNot":"Angular never merges colliding values; one value wins according to the collision rules."},{"id":"f","text":"Angular reports a compile error because the attribute is bound twice.","whyNot":"The guide describes collisions as resolved by fixed rules, not reported as errors."}],"answer":"ab","explanation":"On a collision, a dynamic value beats a static one, two static values let the instance win, and two dynamic values let the component's host binding win.","concept":"On a collision, a dynamic value beats a static one, two static values let the instance win, and two dynamic values let the component's host binding win.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"<!-- host: role='presentation' -->\n<!-- host: [id]='id' -->\n<profile-photo role='group'\n  [id]='otherId' />","takeaways":["When a template and a host set the same attribute, the collision rules decide the winner."]}]