Question
I am using Angular 4 with Angular CLI, and I can create a new component with this command:
ng generate component plainsight
However, I want to generate a child component inside the plainsight folder. Is there a way to do this with Angular CLI?
Short Answer
By the end of this page, you will understand how Angular CLI generates components inside specific folders, how nested component paths work, and how to create child components cleanly within an Angular project structure.
Concept
Angular CLI can generate files not only by component name, but also by path. When you pass a nested path to ng generate component, the CLI creates the component inside that folder and updates your Angular module if needed.
This matters because Angular applications usually grow into many features and sub-features. If every component is created in one flat folder, the project quickly becomes hard to navigate. Organizing components into feature folders makes the codebase easier to maintain.
For example, instead of generating:
ng generate component plainsight
You can generate a child component inside that folder with a nested path such as:
ng generate component plainsight/child
This tells Angular CLI:
- create a folder named
childinsideplainsight - generate the component files there
- declare the component in the appropriate module
In Angular projects, the generated structure often follows the path you provide. So the CLI is not limited to top-level components; it can generate nested components as part of a feature structure.
Mental Model
Think of Angular CLI like a file assistant that builds folders and files based on the address you give it.
plainsightmeans: create one room calledplainsightplainsight/childmeans: go into theplainsightroom and create another room calledchild
The command is not just naming the component. It is also describing where it should live in your project.
So if you want a child component, you give the CLI a deeper path, just like saving a file into a subfolder on your computer.
Syntax and Examples
The basic syntax is:
ng generate component path/to/component-name
You can also use the shorter alias:
ng g c path/to/component-name
Example 1: Generate a top-level component
ng g c plainsight
This usually creates:
plainsight/
plainsight.component.ts
plainsight.component.html
plainsight.component.css
plainsight.component.spec.ts
Example 2: Generate a child component inside plainsight
ng g c plainsight/child
This usually creates:
plainsight/
child/
child.component.ts
child.component.html
child.component.css
child.component.spec.ts
Example 3: Generate a more descriptive nested component
ng g c plainsight/plainsight-detail
This creates a child component named PlainsightDetailComponent inside the plainsight folder.
What Angular CLI does for you
Step by Step Execution
Consider this command:
ng g c plainsight/child
Here is what happens step by step:
- Angular CLI reads
plainsight/childas a path. - It looks for the
plainsightfolder inside your app structure. - Inside that folder, it creates a new folder named
childif it does not already exist. - It generates files such as:
child.component.ts
child.component.html
child.component.css
child.component.spec.ts
- It creates a component class similar to:
import { Component } from '@angular/core';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent {}
- It adds
ChildComponentto thedeclarationsarray of the relevant Angular module.
Real World Use Cases
Nested component generation is useful in many common Angular scenarios:
- Feature folders: Keep all components related to a feature together, such as
users/user-listandusers/user-detail - Dashboard widgets: Generate
dashboard/chart-cardordashboard/activity-feed - Admin pages: Create
admin/user-tableoradmin/role-editor - E-commerce UI: Use folders like
product/product-cardandproduct/product-review - Form sections: Organize large forms with components like
checkout/address-formandcheckout/payment-form
This kind of structure helps teams work faster because developers can quickly find related files.
Real Codebase Usage
In real Angular codebases, developers often organize components by feature, not by type.
For example:
app/
plainsight/
plainsight.component.ts
child/
child.component.ts
settings/
settings.component.ts
Common patterns include:
- Feature-based folders: everything for one feature lives together
- Container and presentational components: a parent component contains smaller child components
- Guarded generation: developers generate components directly into the intended folder to avoid later moving files by hand
- Consistent naming: names like
user-list,user-card, anduser-detailmake the structure predictable - Modular organization: components are often generated under a specific module such as
admin/users/user-form
This reduces confusion, especially in larger projects with dozens or hundreds of components.
Common Mistakes
1. Generating the component in the wrong location
Broken approach:
ng g c child
If you run this from the project root without a path, Angular CLI may generate child as a top-level component instead of placing it inside plainsight.
Use:
ng g c plainsight/child
2. Confusing folder nesting with component hierarchy
Creating a component inside a folder does not automatically make it a child in Angular's rendering tree.
For example, this command:
ng g c plainsight/child
creates a nested folder structure, but the child component will only appear inside PlainsightComponent if you actually use its selector in the parent template.
3. Forgetting to include the child selector in the parent template
Generated child component:
selector: 'app-child'
Parent template must use it:
<app-child></app-child>
Comparisons
| Approach | Example | What it does | Best use |
|---|---|---|---|
| Top-level generation | ng g c plainsight | Creates a component in its own folder | New feature root component |
| Nested generation | ng g c plainsight/child | Creates a component inside plainsight | Child or related feature component |
| Manual file creation | Create files yourself | You control everything manually | Rarely needed for beginners |
| CLI alias | ng g c plainsight/child | Same as full command, shorter syntax | Faster everyday use |
| Concept |
|---|
Cheat Sheet
# Full command
ng generate component plainsight/child
# Short alias
ng g c plainsight/child
Key idea
- Pass a path to Angular CLI, not just a component name.
- The path determines the folder structure.
Examples
ng g c users/user-list
ng g c users/user-detail
ng g c dashboard/activity-feed
Important rules
- Use
/to indicate nested folders - Angular CLI usually creates missing folders automatically
- The CLI usually updates the Angular module for you
- Folder nesting does not automatically render one component inside another
To make it a visual child component
- Generate it in the parent folder
- Find its selector in the generated component
- Add that selector to the parent component template
Common command pattern
ng g c feature-name/sub-component-name
FAQ
Can Angular CLI generate a component inside an existing folder?
Yes. Pass the folder path as part of the component name, such as ng g c plainsight/child.
Does generating inside a folder automatically make it a child component?
No. It only places the files inside that folder. To render it as a child, you must use its selector in the parent template.
Can Angular CLI create the folder if it does not exist?
Yes, Angular CLI typically creates the folder structure for the path you provide.
What is the short version of ng generate component?
You can use ng g c.
Should I use slashes in the component path?
Yes. Use forward slashes like plainsight/child to indicate nested folders.
Will Angular CLI update the module automatically?
In typical Angular CLI setups, yes. It usually adds the generated component to the nearest module declarations.
Is folder structure the same as component parent-child relationship?
No. Folder structure is for file organization. Parent-child rendering happens in templates using selectors.
Mini Project
Description
Create a small Angular feature called profile with a nested child component called profile-card. This project demonstrates how Angular CLI uses paths to generate components inside specific folders and how a parent component can render a child component using its selector.
Goal
Build a parent profile component and a nested profile-card component, then render the child inside the parent template.
Requirements
Create a profile component using Angular CLI.
Generate a profile-card component inside the profile folder.
Display the child component inside the parent component template.
Add simple text to both templates so the relationship is visible.
Keep learning
Related questions
@Directive vs @Component in Angular: Differences, Use Cases, and When to Use Each
Learn the difference between @Directive and @Component in Angular, including use cases, examples, and when to choose each.
Angular (change) vs (ngModelChange): What’s the Difference?
Learn the difference between Angular (change) and (ngModelChange), when each fires, and which one to use in forms and inputs.
Angular @ViewChild Returning Undefined: Lifecycle, Child Components, and Fixes
Learn why Angular @ViewChild can be undefined, when it becomes available, and how to access child components correctly using lifecycle hooks.