0
0
Angularframework~20 mins

Custom structural directives in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Structural Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this custom structural directive render?
Given the following Angular structural directive usage, what will be rendered in the browser?
Angular
/* Directive code omitted for brevity */
@Component({
  selector: 'app-root',
  template: `
    <div *appUnless="condition">Show this if condition is false</div>
  `
})
export class AppComponent {
  condition = false;
}
AThe text 'Show this if condition is false' is displayed
BNothing is displayed
CAn error occurs because 'appUnless' is not a built-in directive
DThe text 'Show this if condition is false' is displayed only if condition is true
Attempts:
2 left
💡 Hint
Think about what the directive name 'appUnless' suggests about when it shows content.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a custom structural directive selector?
You want to create a custom structural directive named 'appShowIf'. Which selector syntax is correct?
A"*appShowIf"
B"[appShowIf]"
C"appShowIf"
D"[appShowIf]" and use *appShowIf in template
Attempts:
2 left
💡 Hint
Structural directives use a special syntax in templates but their selectors are attribute selectors.
lifecycle
advanced
2:00remaining
When is the ngOnChanges lifecycle hook called in a custom structural directive?
Consider a custom structural directive with an @Input() property. When will Angular call ngOnChanges in this directive?
AOnly when the input property changes after initialization
BOnly once when the directive is created
CWhenever the input property value changes, including the first time it is set
DngOnChanges is not called in structural directives
Attempts:
2 left
💡 Hint
Think about how Angular detects changes to input properties.
🔧 Debug
advanced
2:30remaining
Why does this custom structural directive cause a runtime error?
This directive tries to create an embedded view but causes an error. What is the likely cause? ```typescript @Directive({ selector: '[appShow]' }) export class ShowDirective { constructor(private templateRef: TemplateRef) {} ngOnInit() { this.templateRef.createEmbeddedView(); } } ```
AMissing @Input property to control rendering
BcreateEmbeddedView is a method of ViewContainerRef, not TemplateRef
CThe selector should be '*appShow' instead of '[appShow]'
DThe directive must inject ChangeDetectorRef to create views
Attempts:
2 left
💡 Hint
Check which Angular class has the createEmbeddedView method.
state_output
expert
3:00remaining
What is the output count of items rendered by this custom structural directive?
Given this directive and component, how many
  • elements will be rendered? ```typescript @Directive({ selector: '[appRepeat]' }) export class RepeatDirective { @Input() set appRepeat(times: number) { this.viewContainer.clear(); for (let i = 0; i < times; i++) { this.viewContainer.createEmbeddedView(this.templateRef, { index: i }); } } constructor(private templateRef: TemplateRef, private viewContainer: ViewContainerRef) {} } @Component({ selector: 'app-root', template: `
    • Item {{index}}
    ` }) export class AppComponent {} ```
  • A3 <li> elements with text 'Item 0', 'Item 1', and 'Item 2'
    B1 <li> element with text 'Item 3'
    C3 <li> elements all showing 'Item undefined'
    DNo <li> elements rendered because of missing context
    Attempts:
    2 left
    💡 Hint
    Look at how many times the view is created and what context is passed.