The Angular CLI (Command Line Interface) is the primary tool for scaffolding, developing, and building Angular applications. This chapter covers CLI commands, configuration, decorators, lifecycle hooks, and production deployment.
The Angular CLI simplifies every phase of development. Below are the most important commands:
Scaffold components, directives, pipes, services, modules, and more.
ng generate component dashboard
ng generate directive highlight
ng generate pipe filter
ng generate service data
ng generate module admin --route admin --module app.module
ng generate class models/user
ng generate interface models/user
ng generate guard auth
Start a development server with live reload and source maps.
ng serve --open --port 4200 --hmr
Compile the application into output files in the dist/ folder.
ng build --prod --output-path docs --base-href /my-app/
Run unit tests with Karma and end-to-end tests with Protractor or Cypress.
ng test --watch=false --code-coverage
ng e2e
Deploy to various cloud providers with a single command.
ng deploy --base-href=/my-app/
The angular.json file defines the workspace structure, project configurations, build options, and environments.
{
"projects": {
"my-app": {
"architect": {
"build": {
"configurations": {
"production": {
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{ "type": "initial", "maximumWarning": "2mb" },
{ "type": "anyComponentStyle", "maximumWarning": "6kb" }
]
}
}
}
}
}
}
}
Decorators add metadata to classes, methods, and properties. They are the building blocks of Angular's declarative syntax.
@Component({ selector: 'app-root', templateUrl: './app.component.html' })
@Directive({ selector: '[appHighlight]' })
@Pipe({ name: 'filter' })
@Injectable({ providedIn: 'root' })
@Input() user: User;
@Output() clicked = new EventEmitter<void>();
Angular components and directives have lifecycle hooks that let you tap into key moments:
ngOnChanges — Called when input properties changengOnInit — Called once after the first ngOnChangesngDoCheck — Called during every change detection runngAfterContentInit — After content projectionngAfterContentChecked — After every content checkngAfterViewInit — After the view and child views are initializedngAfterViewChecked — After every view checkngOnDestroy — Cleanup before Angular destroys the componentOverride Angular's ErrorHandler to provide global error handling.
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
handleError(error: any) {
console.error('Global error:', error);
// Send to logging service
}
}
// app.module.ts
providers: [{ provide: ErrorHandler, useClass: GlobalErrorHandler }]
Use --prod to enable AOT compilation, tree-shaking, minification, and dead code elimination.
ng build --prod
This produces optimized bundles in dist/. Deploy these files to any static hosting or cloud platform.
angular.json to set a budget warning of 1 MB for the initial bundle. Build the application in production mode and inspect the output in the dist/ folder.