Challenge - 5 Problems
Angular Enums Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Angular handle enum values in templates?
Given this enum and component code, what will be displayed in the template?
export enum Status {
Active = 'ACTIVE',
Inactive = 'INACTIVE'
}
@Component({
selector: 'app-status',
template: `<p>Status: {{ status }}</p>`
})
export class StatusComponent {
status = Status.Active;
}Angular
export enum Status { Active = 'ACTIVE', Inactive = 'INACTIVE' } @Component({ selector: 'app-status', template: `<p>Status: {{ status }}</p>` }) export class StatusComponent { status = Status.Active; }
Attempts:
2 left
💡 Hint
Remember enums with string values show the string, not the key name.
✗ Incorrect
In Angular templates, when you bind an enum value that is a string, it displays the string value. Here, Status.Active is 'ACTIVE', so the template shows 'Status: ACTIVE'.
📝 Syntax
intermediate2:00remaining
Which enum declaration is valid in Angular TypeScript?
Choose the correct enum declaration syntax that will compile without errors in Angular.
Attempts:
2 left
💡 Hint
Look for the correct enum key-value syntax in TypeScript.
✗ Incorrect
Option C uses the correct enum syntax with keys and string values separated by = and commas. Options B, C, and D have syntax errors.
🔧 Debug
advanced2:00remaining
Why does this Angular component throw an error when using an enum in the template?
Consider this code snippet:
What is the cause of the error when rendering the template?
export enum Mode {
Light,
Dark
}
@Component({
selector: 'app-theme',
template: `<p>Current mode: {{ Mode.Dark }}</p>`
})
export class ThemeComponent {
}What is the cause of the error when rendering the template?
Angular
export enum Mode { Light, Dark } @Component({ selector: 'app-theme', template: `<p>Current mode: {{ Mode.Dark }}</p>` }) export class ThemeComponent { }
Attempts:
2 left
💡 Hint
Think about how templates access class properties and variables.
✗ Incorrect
Angular templates can only access properties of the component class instance. Without assigning the enum to a class property, the template cannot see it, causing an error.
❓ state_output
advanced2:00remaining
What is the value of 'currentStatus' after this Angular component runs?
Given this enum and component code:
What is the value of 'currentStatus'?
export enum Status {
Pending = 1,
Approved,
Rejected
}
@Component({
selector: 'app-request',
template: `<p>Status: {{ currentStatus }}</p>`
})
export class RequestComponent {
currentStatus = Status.Approved;
}What is the value of 'currentStatus'?
Angular
export enum Status { Pending = 1, Approved, Rejected } @Component({ selector: 'app-request', template: `<p>Status: {{ currentStatus }}</p>` }) export class RequestComponent { currentStatus = Status.Approved; }
Attempts:
2 left
💡 Hint
Numeric enums auto-increment from the first value.
✗ Incorrect
Status.Pending is 1, so Status.Approved is 2, and Status.Rejected is 3. The value of currentStatus is 2.
🧠 Conceptual
expert2:00remaining
Why should enums be assigned to component properties for template use in Angular?
In Angular, why is it necessary to assign an enum to a component property before using it in the template?
Attempts:
2 left
💡 Hint
Think about the scope of variables accessible in Angular templates.
✗ Incorrect
Angular templates only have access to the component's instance properties and methods. Imported enums are not visible unless assigned to a property.