What is a component in Angular? How do you create one?

Beginner

Answer

A component in Angular controls a part of the screen called a view. It consists of:

  • Component Class: Contains the application logic (TypeScript)
  • Template: Defines the view (HTML)
  • Styles: Define the appearance (CSS)
  • Metadata: Tells Angular how to process the class (Decorators)

Creating a component:

import { Component } from '@angular/core';

@Component({
  selector: 'app-example',
  template: '<h1>{{title}}</h1>',
  styleUrls: ['./example.component.css']
})
export class ExampleComponent {
  title = 'Hello World';
}