import { Component, NgModule } from '@angular/core'; @Component({ selector: 'app-root', template: `<h1>{{ title }}</h1>` }) export class AppComponent { title = 'Hello Angular'; } @NgModule({ declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {}
The AppComponent is declared and bootstrapped in AppModule. The template renders the title property inside an <h1> tag, so the output is <h1>Hello Angular</h1>.
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { SharedComponent } from './shared.component'; @NgModule({ declarations: [SharedComponent], imports: [CommonModule] }) export class SharedModule {}
The exports property is needed to make SharedComponent usable in other modules that import SharedModule. Without it, the component is private to this module.
FeatureComponent.import { NgModule } from '@angular/core'; import { FeatureComponent } from './feature.component'; @NgModule({ declarations: [], imports: [], bootstrap: [FeatureComponent] }) export class FeatureModule {}
Components must be declared in the declarations array to be recognized by the module. Bootstrapping a component not declared causes a runtime error.
The providers array registers services that Angular can inject into components and other services within the module.
When a service is provided in a shared module (not at root), importing that module multiple times creates separate instances of the service for each importing module.
