liveBook · Manning

Imports property

As explained in Modern Angular

The imports array is Angular component metadata that lists modules, standalone components, directives, and pipes available to that component.

The imports property is component metadata for declaring dependencies available to a standalone component. It replaces NgModule-based dependency lookup by keeping required modules, components, directives, and pipes beside the component. section 1.3.3 section 2.3.1

Older Angular components commonly received dependencies through NgModules. Standalone components instead import what their templates use directly, reducing broad imports such as CommonModule when a single directive is sufficient. section 1.3.3 section 2.3.1

Use the property to:

  • Import an NgModule such as FormsModule when the template uses ngModel. section 2.3.1
  • Import standalone directives such as NgIf instead of importing all of CommonModule. section 2.3.1
  • List standalone components, directives, and pipes required by the component template. section 1.3.3

Login form with focused imports

A standalone LoginComponent can declare imports: [FormsModule, NgIf]. FormsModule enables [(ngModel)] for email and password fields, while NgIf enables the validation message shown when either credential is missing. The component can still use an NgModule dependency and a standalone directive together. section 2.3.1

Listing 2.7 Standalone component importing a module
@Component({
    selector: 'app-login',
    template: `
        <div class="login-container">
            <h1>Login</h1>
            <form>
                <input type="text" name="email"
                    placeholder="Email"
                    [(ngModel)]="credentials.email" />    #1
                <input type="password" name="password" placeholder="Password" 
                   [(ngModel)]="credentials.password">
                <button type="submit">Login</button>
            </form>
        </div>
    `,
    standalone: true,
    imports: [FormsModule],     #2
})
export class LoginComponent {
    credentials = { email: '', password: '' };
}

Inspect a complete standalone component using FormsModule through its imports property and binding form fields with ngModel. section 2.3.1

For migration, author new declarations as standalone, convert existing components individually, move their NgModule declaration into imports, preserve exports where needed, and remove modules incrementally as build errors are resolved. section 2.5.1

How Modern Angular covers it

How deep it goes
Explains metadata usage through FormsModule and NgIf examples, then extends into incremental migration mechanics.
Examples
Uses a standalone login form with ngModel bindings, NgIf validation, FormsModule, and a root AppComponent importing RouterOutlet.
What it assumes
Assumes familiarity with Angular components and NgModules while teaching standalone dependency declaration from the component level.
What you can do after
Apply it via Angular component metadata to assemble standalone templates and migrate NgModule declarations incrementally.
How it connects
Connects standalone components with NgModules, FormsModule, CommonModule, directives, and pipes.

Also explained in

Compare how 2 books explain imports property →

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.