What is the purpose of the @Component decorator in Angular?
The @Component decorator marks a class as an Angular component and provides metadata that tells Angular how to process, instantiate, and use the component in the app.
Click to reveal answer
beginner
Name three common metadata properties used inside the @Component decorator.
Common metadata properties include: - selector: the HTML tag to use the component - templateUrl or template: the HTML view for the component - styleUrls or styles: CSS styles for the component
Click to reveal answer
beginner
How does the 'selector' property in @Component metadata affect the component?
The 'selector' defines the custom HTML tag that represents the component. When Angular sees this tag in HTML, it creates and inserts the component there.
Click to reveal answer
intermediate
What is the difference between 'template' and 'templateUrl' in component metadata?
'template' contains inline HTML code as a string inside the component metadata. 'templateUrl' points to an external HTML file that contains the component's template.
Click to reveal answer
beginner
Why is metadata important in Angular components?
Metadata tells Angular how to create and display the component, what HTML tag to use, what template and styles to apply, and how to connect the component to the app.
Click to reveal answer
Which decorator is used to define an Angular component?
A@Component
B@Injectable
C@Directive
D@NgModule
✗ Incorrect
The @Component decorator is specifically used to define Angular components.
What does the 'selector' property in @Component metadata specify?
AThe CSS styles for the component
BThe external JavaScript file for the component
CThe HTML tag name to use the component
DThe component's class name
✗ Incorrect
The 'selector' defines the custom HTML tag that represents the component.
Which property would you use to link an external HTML file as the component's template?
AstyleUrls
BtemplateUrl
Ctemplate
Dstyles
✗ Incorrect
'templateUrl' points to an external HTML file for the component's template.
Can you use inline CSS styles inside the @Component decorator metadata?
AYes, using the 'styles' property
BOnly with the 'template' property
CNo, styles must be in external files
DOnly in the main app module
✗ Incorrect
You can use the 'styles' property to add inline CSS styles directly in the component metadata.
What happens if you forget to add a selector in the @Component metadata?
AThe component will throw a runtime error
BAngular will use a default selector
CThe component will automatically render everywhere
DThe component cannot be used in HTML templates
✗ Incorrect
Without a selector, Angular does not know what HTML tag to use for the component, so it cannot be inserted in templates.
Explain the role of the @Component decorator and its metadata in Angular.
Think about how Angular knows what a component is and how to display it.
You got /5 concepts.
Describe the difference between 'template' and 'templateUrl' in component metadata and when you might use each.
Consider how you organize your HTML code for components.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @Component decorator in Angular?
easy
A. To create a routing configuration
B. To define a service for dependency injection
C. To mark a class as an Angular component and provide metadata
D. To import external modules
Solution
Step 1: Understand the role of decorators in Angular
Decorators add metadata to classes, telling Angular how to use them.
Step 2: Identify what @Component specifically does
@Component marks a class as a component and provides metadata like selector and template.
Final Answer:
To mark a class as an Angular component and provide metadata -> Option C
Quick Check:
@Component marks component class = C [OK]
Hint: Remember: @Component decorates component classes only [OK]
Common Mistakes:
Confusing @Component with @Injectable
Thinking @Component defines routing
Assuming @Component imports modules
2. Which of the following is the correct way to specify the HTML template inline in a component decorator?
easy
A. @Component({ template: '
Hello
' })
B. @Component({ templateUrl: './app.component.html' })
C. @Component({ templateFile: 'app.component.html' })
D. @Component({ htmlTemplate: '
Hello
' })
Solution
Step 1: Recall the difference between template and templateUrl
template is for inline HTML, templateUrl is for external file path.
Step 2: Identify the correct syntax for inline template
Only template: '
Hello
' is valid for inline HTML.
Final Answer:
@Component({ template: '<h1>Hello</h1>' }) -> Option A
Quick Check:
Inline HTML uses template property = A [OK]
Hint: Use 'template' for inline HTML, 'templateUrl' for files [OK]
Common Mistakes:
Using templateUrl with inline HTML
Using wrong property names like templateFile
Confusing template and htmlTemplate
3. Given this component decorator:
@Component({ selector: 'app-hello', template: '
{{ message }}
' }) export class HelloComponent { message = 'Hi!'; }
What will be rendered in the browser when <app-hello></app-hello> is used?
medium
A.
{{ message }}
B.
Hi!
C. An empty paragraph
D. Error: message is undefined
Solution
Step 1: Understand Angular interpolation
The double curly braces {{ message }} bind to the component's property 'message'.
Step 2: Check the value of 'message' in the component
It is set to 'Hi!', so Angular replaces {{ message }} with 'Hi!'.
Final Answer:
<p>Hi!</p> -> Option B
Quick Check:
Interpolation shows property value = A [OK]
Hint: {{ }} shows property value, not raw text [OK]
Common Mistakes:
Thinking interpolation shows raw {{ message }}
Assuming message is undefined without constructor
Expecting empty output without initialization
4. Identify the error in this component decorator:
D. The styles property should be styleUrls for external CSS files
Solution
Step 1: Check the use of styles vs styleUrls
styles is for inline CSS strings; styleUrls is for external CSS files.
Step 2: Identify the mistake in the code
The code uses styles with a file path array, which is incorrect; it should be styleUrls.
Final Answer:
The styles property should be styleUrls for external CSS files -> Option D
Quick Check:
External CSS uses styleUrls, not styles = D [OK]
Hint: Use styleUrls for files, styles for inline CSS [OK]
Common Mistakes:
Confusing styles and styleUrls
Thinking styles accepts file paths
Adding @Injectable to components
5. You want to create a reusable button component with the selector app-button that uses an external HTML template and multiple CSS files. Which is the correct component decorator configuration?
hard
A. @Component({ selector: 'app-button', templateUrl: './button.html', styleUrls: ['./btn.css', './colors.css'] })
B. @Component({ selector: 'app-button', template: 'Click', styles: ['./btn.css', './colors.css'] })
C. @Component({ selector: 'app-button', templateUrl: './button.html', styles: ['./btn.css', './colors.css'] })
D. @Component({ selector: 'app-button', template: './button.html', styleUrls: ['./btn.css', './colors.css'] })
Solution
Step 1: Identify how to specify external HTML template
Use templateUrl with the path to the HTML file.
Step 2: Identify how to specify multiple external CSS files
Use styleUrls with an array of CSS file paths.
Step 3: Check each option for correct usage
The configuration using templateUrl: './button.html' and styleUrls: ['./btn.css', './colors.css'] is correct. Others incorrectly use template for a file path or styles for external CSS files.