Ir al contenido

Components

Esta página aún no está disponible en tu idioma.

Components are the fundamental building blocks of Angular applications. Each component encapsulates its own template, logic, and styles into a self-contained, reusable unit. This section covers everything from basic component creation to advanced patterns in Angular 18+.



FeatureLegacy SyntaxModern Syntax (Angular 18+)
Inputs@Input() name: stringname = input<string>()
Required inputs@Input({ required: true })name = input.required<string>()
Outputs@Output() event = new EventEmitter()event = output<T>()
Two-way binding@Input() + @Output() pairvalue = model<T>()
View queries@ViewChild() / @ViewChildren()viewChild() / viewChildren()
Content queries@ContentChild() / @ContentChildren()contentChild() / contentChildren()
Conditionals*ngIf@if
Loops*ngFor@for (item of items; track item.id)
Switch[ngSwitch]@switch
Standalonestandalone: trueDefault (flag not needed)
BootstrappingbootstrapModule(AppModule)bootstrapApplication(AppComponent, appConfig)
Lazy loadingloadChildren: () => ModuleloadComponent: () => Component
Async dataManual HTTP + subscriberesource() / rxResource()
Zonezone.js (implicit)Zoneless with provideExperimentalZonelessChangeDetection()

  1. Keep components focused — Each component should have a single responsibility.
  2. Use signal inputs/outputs — Prefer input(), output(), and model() over decorators.
  3. Always use OnPush — Better performance and predictable change detection.
  4. Use descriptive selectors — Prefer app-user-card over app-uc.
  5. Prefer external templates and styles — Better maintainability for larger components.
  6. Clean up resources — Use DestroyRef or ngOnDestroy to free subscriptions/timers.
  7. Leverage the CLI — Always use ng generate component for consistent file structure.
  8. Use track in @for loops — Always provide a tracking expression for performance.
  9. Test dynamic components — Verify dynamically loaded components render correctly.
  10. Use content projection — Build flexible, reusable container components.