current version: 12 - Date: Oct 2021
Bootstrapping
Bootstraps the app, using the root component from the specified NgModule.
import { platformBrowserDynamic }
from '@angular/platform-browser-dynamic';
platformBrowserDynamic()
.bootstrapModule(AppModule);
NgModule
Defines a module that contains components, directives, pipes, and providers.
import { NgModule } from '@angular/core';
@NgModule({
declarations: [
MyRootComponent,
MyComponent,
MyDirective,
MyPipe],
imports: [MyModule, NpmModule],
exports: [MyComponent],
providers: [MyService],
bootstrap: [MyRootComponent] // Only root module
})
class MyModule {}
Parameter | Function |
---|---|
declarations | Components to this module |
imports | Modules to import into this module |
exports | Components visible to another module |
providers | Dependency injection providers |
bootstrap | The root component of module |
The pipes and directive is declared as components
Class Type
Component
@Component({
selector: 'app-my',
templateUrl: './my.component.html',
styleUrls: ['./my.component.css']
})
class MyComponent() {}
Directive
@Directive({
selector: ['appMy'],
templateUrl: './my.directive.html',
styleUrls: ['./my.directive.css']
})
class MyDirective() {}
Pipe
@Pipe({name: 'myPipe'})
class MyPipe() {}
Provider
@Injectable()
class MyService() {}
Add {providedIn: root} option for create singleton
Component interaction
Input and Output
export class InventoryComponent {
@Input() hero: Hero;
@Input('newItem') itemToInsert: Item;
@Output() itemSelectionned =
new EventEmitter<Item>();
onClickHero(item: Item) {
this.itemSelectionned.emit(item);
}
}