The Component decorator tells Angular that a class is a component. It also gives extra information (metadata) about how the component should work and look.
Component decorator and metadata in Angular
@Component({
selector: 'app-name',
templateUrl: './app-name.component.html',
styleUrls: ['./app-name.component.css']
})
export class AppNameComponent {
// component logic here
}The @Component decorator is placed just above the class.
The object inside @Component({ ... }) holds metadata like selector, template, and styles.
@Component({
selector: 'app-hello',
template: '<h1>Hello World!</h1>'
})
export class HelloComponent {}@Component({
selector: 'app-user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent {}@Component({
selector: 'app-button',
template: '<button>Click me</button>',
styles: ['button { color: blue; }']
})
export class ButtonComponent {}This component shows a heading and a paragraph with some styles. The selector app-greeting is used to place this component in HTML.
import { Component } from '@angular/core'; @Component({ selector: 'app-greeting', template: `<h2>Welcome to Angular!</h2><p>This is a simple component.</p>`, styles: [`h2 { color: green; } p { font-style: italic; }`] }) export class GreetingComponent {}
The selector is the HTML tag you use to add the component in your app.
You can use either template for inline HTML or templateUrl for an external file.
Similarly, use styles for inline CSS or styleUrls for external CSS files.
The @Component decorator marks a class as an Angular component.
Metadata inside the decorator tells Angular how to display and style the component.
Use selector, template/templateUrl, and styles/styleUrls to define your component's look and usage.
Practice
@Component decorator in Angular?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
@Componentspecifically does@Componentmarks 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 CQuick Check:
@Component marks component class = C [OK]
- Confusing @Component with @Injectable
- Thinking @Component defines routing
- Assuming @Component imports modules
Solution
Step 1: Recall the difference between template and templateUrl
templateis for inline HTML,templateUrlis for external file path.Step 2: Identify the correct syntax for inline template
Onlytemplate: 'is valid for inline HTML.Hello
'Final Answer:
@Component({ template: '<h1>Hello</h1>' }) -> Option AQuick Check:
Inline HTML uses template property = A [OK]
- Using templateUrl with inline HTML
- Using wrong property names like templateFile
- Confusing template and htmlTemplate
@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?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 BQuick Check:
Interpolation shows property value = A [OK]
- Thinking interpolation shows raw {{ message }}
- Assuming message is undefined without constructor
- Expecting empty output without initialization
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styles: ['./test.component.css']
})
export class TestComponent {}Solution
Step 1: Check the use of styles vs styleUrls
stylesis for inline CSS strings;styleUrlsis for external CSS files.Step 2: Identify the mistake in the code
The code usesstyleswith a file path array, which is incorrect; it should bestyleUrls.Final Answer:
The styles property should be styleUrls for external CSS files -> Option DQuick Check:
External CSS uses styleUrls, not styles = D [OK]
- Confusing styles and styleUrls
- Thinking styles accepts file paths
- Adding @Injectable to components
app-button that uses an external HTML template and multiple CSS files. Which is the correct component decorator configuration?Solution
Step 1: Identify how to specify external HTML template
UsetemplateUrlwith the path to the HTML file.Step 2: Identify how to specify multiple external CSS files
UsestyleUrlswith an array of CSS file paths.Step 3: Check each option for correct usage
The configuration usingtemplateUrl: './button.html'andstyleUrls: ['./btn.css', './colors.css']is correct. Others incorrectly usetemplatefor a file path orstylesfor external CSS files.Final Answer:
@Component({ selector: 'app-button', templateUrl: './button.html', styleUrls: ['./btn.css', './colors.css'] }) -> Option AQuick Check:
External HTML + multiple CSS files = templateUrl + styleUrls = B [OK]
- Using styles for external CSS files
- Using template instead of templateUrl for files
- Mixing inline and external template properties
Ready to prove it?
Pass the test and this pattern moves unknown
