liveBook · Manning

Required key

As explained in Modern Angular

A required input is an Angular input declaration that requires a parent component to provide a value and enables compile-time diagnostics when the value is omitted.

Angular required inputs force parent components to provide designated values and surface omissions during compilation instead of relying on custom runtime errors. section 4.1.1

The modern declaration improves required-value handling by:

  • Marking an input with @Input({required: true}) so the compiler checks every component use. section 4.1.1
  • Showing a clear editor error when a required value such as projectId is omitted. section 4.1.1
  • Removing input-validation boilerplate so the component can focus on business logic. section 4.1.1

ProjectCardComponent

A project card declares projectId as required while continuing to load project data in ngOnChanges. Omitting the binding at the call site produces an immediate required-input diagnostic. section 4.1.1

Listing 4.2 Input marked as required in the ProjectCardComponent
export class ProjectCardComponent implements OnChanges {
    private readonly projectService = inject(ProjectService);

    @Input({required: true}) projectId!: number;   #1

    project$: Observable<Project> | null = null;

    ngOnChanges(changes: SimpleChanges): void {
        if (changes['projectId']) {
            this.project$ = this.projectService.getProject(this.projectId);
        }
    }
}

Inspect the required-input declaration and its surrounding component logic. section 4.1.1

How Modern Angular covers it

How deep it goes
Contrasts runtime validation with a concrete declaration and compile-time template diagnostic, without exploring compiler internals.
Examples
Uses ProjectCardComponent and its required projectId input as the concrete implementation example.
What it assumes
Builds on Angular input properties and component templates.
What you can do after
Apply it via Angular input declarations to catch omitted parent bindings in the editor.
How it connects
Connects Angular input properties with component templates, compiler diagnostics, and lifecycle hooks.

Also explained in

Compare how 3 books explain required key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern Angular. The section links point to the parts of the book it draws on.