0
0
Angularframework~20 mins

Component decorator and metadata in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Component Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Angular component render?
Consider this Angular component code. What will be displayed in the browser when this component is used?
Angular
import { Component } from '@angular/core';

@Component({
  selector: 'app-greet',
  template: `<h1>Hello, {{name}}!</h1>`,
  styles: [`h1 { color: blue; }`]
})
export class GreetComponent {
  name = 'Alice';
}
A<h1>Hello, Alice!</h1> with blue text
BError because styles property is invalid
CNothing renders because of missing selector
D<h1>Hello, {{name}}!</h1> with default black text
Attempts:
2 left
💡 Hint
Look at the template and the value of the 'name' property.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a component with inline template and styles?
Choose the correct Angular component decorator syntax that uses inline template and styles.
A@Component({ selector: 'app-test', templateUrl: 'test.html', styles: ['p { color: red; }'] })
B@Component({ selector: 'app-test', template: '<p>Test</p>', styles: ['p { color: red; }'] })
C@Component({ selector: 'app-test', template: '<p>Test</p>', styleUrls: ['test.css'] })
D@Component({ selector: 'app-test', templateUrl: 'test.html', styleUrls: ['test.css'] })
Attempts:
2 left
💡 Hint
Inline template uses 'template' property, not 'templateUrl'. Inline styles use 'styles', not 'styleUrls'.
🔧 Debug
advanced
2:00remaining
What error does this component code produce?
Examine this Angular component code. What error will Angular throw when compiling or running this component?
Angular
import { Component } from '@angular/core';

@Component({
  selector: 'app-error',
  template: `<p>{{message}}</p>`
})
export class ErrorComponent {
  constructor() {
    console.log(this.message.length);
  }
  message: string;
}
ASyntax error: Missing semicolon after message declaration
BNo error, outputs empty paragraph
CRuntime error: Cannot read property 'length' of undefined
DCompile error: Template syntax invalid
Attempts:
2 left
💡 Hint
Consider when the constructor runs and if 'message' is initialized.
🧠 Conceptual
advanced
1:30remaining
What is the purpose of the 'selector' property in @Component?
In Angular's @Component decorator, what does the 'selector' property do?
ADefines the HTML tag name to use this component in templates
BSpecifies the CSS selector to style the component
CSets the component's class name for debugging
DIndicates the file path of the component's template
Attempts:
2 left
💡 Hint
Think about how you add this component inside other HTML or templates.
state_output
expert
2:00remaining
What is the final value of 'count' after this component runs?
Given this Angular component, what is the value of 'count' after the component initializes?
Angular
import { Component } from '@angular/core';

@Component({
  selector: 'app-counter',
  template: `<button (click)='increment()'>Count: {{count}}</button>`
})
export class CounterComponent {
  count = 0;

  constructor() {
    this.increment();
  }

  increment() {
    this.count += 1;
  }
}
AUndefined
B0
C2
D1
Attempts:
2 left
💡 Hint
The constructor calls increment once before rendering.