← Back to Angular, page 1

angular-0002 · Easy fresher

What is an Angular component, and what do the main properties of the @Component decorator do?

A component is a TypeScript class marked with @Component that owns a template, optional styles, and the data and methods the template uses. The decorator tells Angular its selector, the template, the styles, and which other components, directives, and pipes the template may use.

Full answer

A component is the basic piece of an Angular screen. It has three parts: a class (the data and behaviour), a template (the HTML), and styles (the CSS). The @Component decorator joins them and tells Angular how to use the class.

The properties you meet most often:

  • selector: the custom HTML tag name, for example app-score-card. Using that tag in another template creates the component there.
  • template or templateUrl: the HTML. Inline template suits short markup. templateUrl points to a separate file.
  • styles or styleUrl: the CSS. By default styles are scoped to this component only, because Angular adds a unique attribute to its elements (view encapsulation, emulated mode).
  • imports: the standalone components, directives, and pipes that this template is allowed to use. If the template uses <app-badge> and it is not in imports, the compiler reports an error.
  • changeDetection: Default or OnPush, which decides how often Angular re-checks this component.

Each time you write <app-score-card> Angular creates a new instance of the class, so two score cards on one page have separate data.

Style scoping is a common surprise. A rule such as h2 { color: red } inside a component does not touch an h2 in another component.

typescript
import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
  selector: 'app-badge',
  template: `<span class="badge">{{ label }}</span>`,
  styles: `
    .badge {
      background: #1a73e8;
      color: white;
      padding: 2px 8px;
      border-radius: 8px;
    }
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BadgeComponent {
  label = 'Passed';
}

@Component({
  selector: 'app-result-row',
  imports: [BadgeComponent],
  template: `
    <p>{{ student }} scored {{ marks }} <app-badge /></p>
  `,
})
export class ResultRowComponent {
  student = 'Asha';
  marks = 88;
}

Angular 20.3. The score card can be used as `<app-score-card subject="Maths" [marks]="92" />` in a parent template if the parent imports it.

Key points

  • A component is a class, a template, and styles tied together by `@Component`.
  • `selector` is the tag name, and `imports` lists what the template can use.
  • Styles are scoped to the component by default (emulated view encapsulation).
  • Each use of the tag creates a separate instance.

Common mistakes

  • Using a child component in a template without adding it to `imports`. The build fails with an unknown element error.
  • Expecting a global style written inside a component to affect other components.
  • Putting a lot of logic and HTTP calls in the component class. That belongs in a service.

Follow-ups

  • What is the difference between `template` and `templateUrl`?
  • How would you style an element inside a child component from the parent?
  • What does `ViewEncapsulation.None` do?

Tips

  • Give one sentence on each property you mention. A bare list sounds memorised.
  • Say that `standalone: true` is the default in Angular 19 and later, so you do not write it.
  • If asked about styles, mention emulated encapsulation and that it is not a real shadow DOM.

Evaluation rubric

  • Names selector, template, styles, and imports correctly.
  • Understands style scoping.
  • Mentions that logic belongs in services.