How the App Boots and Renders
-
Static Host Page
src/index.htmlprovides the bare HTML page with<app-root></app-root>. This is the DOM node Angular will attach to. -
Entry Script (
main.ts)
```ts import { bootstrapApplication } from '@angular/platform-browser'; import { appConfig } from './app/app.config'; import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
``
-bootstrapApplicationspins up Angular using the standalone API.
-AppComponentis declared as the root component.
-appConfig` registers global providers, including routing and zone-based change detection.
-
App Configuration (
app.config.ts)
ts export const appConfig: ApplicationConfig = { providers: [ provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), ], };- Ensures Angular listens for browser events and runs change detection. - Registers the router with the route definition that points/to the sandbox component. -
Root Component (
app.component.ts)
ts @Component({ selector: 'app-root', standalone: true, imports: [RouterOutlet], template: `<router-outlet />`, }) export class AppComponent {}- When bootstrapped, Angular renders<router-outlet>into<app-root>. - The router then loads theSandboxComponentfor the default route. -
Sandbox Route (
app.routes.ts)
ts export const routes: Routes = [ { path: '', component: SandboxComponent }, ];- Navigating to/instantiatesSandboxComponentand inserts its template into the page.
How the “Show Modal” Button Works
-
Sandbox State
```ts export class SandboxComponent { modalOpen = signal(false);toggleModal(state: boolean): void { this.modalOpen.set(state); } }
`` -signal(false)stores modal visibility. Signals are reactive primitives in Angular 16+. -toggleModal(true)opens the modal;toggleModal(false)` closes it. -
Button Interaction
html <ui-button (click)="toggleModal(true)">Show Modal</ui-button>- When the user clicks, Angular executestoggleModal(true). - The signal updates totrue, markingSandboxComponentfor change detection. -
Modal Template Binding
```html <ui-modal [open]="modalOpen()" title="Component Library" (requestClose)="toggleModal(false)"<!-- modal body -->`` - The customui-modalcomponent receives the signal value via[open]="modalOpen()". - Angular re-evaluates the template after the signal changes and passestrue` to the modal. -
Modal Component Logic
``ts @Component({ selector: 'ui-modal', standalone: true, imports: [CommonModule], template: `, }) export class ModalComponent { @Input() open = false; @Output() requestClose = new EventEmitter{{ title }}
(); onBackdropClick(event: MouseEvent): void { if (event.target === event.currentTarget) { this.requestClose.emit(); } } }
`` - When[open]istrue, the*ngIfrenders the modal markup. - Clicking the close button or backdrop emitsrequestClose, which callstoggleModal(false)` in the sandbox. -
Angular Change Detection Cycle
- The button click runs inside Angular’s zone; change detection runs automatically. - The signal change updates the view, showing the modal instantly without manual DOM manipulation.
Try It Yourself
cd examples/week01-02
npm install
npm start
- Navigate to
http://localhost:4200and click Show Modal to see the signal-driven UI. - Modify text or styling in
ui-modalorui-buttonand watch the updates live via Angular CLI’s HMR.