How to create a new component in Angular?
Last Updated : 24 Apr, 2025
A component in Angular is the building block for making web pages. It is a reusable block of code that can be used anywhere in the app any number of times. It provides scalability, reusability, and readability. Each component does a specific job like showing a menu, a photo or a card, etc.
In this article, we will see how to create a new component in Angular.
Steps to create a new component in Angular:
Step 1: Download and install NodeJS from its official website, npm is included with that.
Step 2: To install angular CLI run the following command in the terminal.
npm install -g @angular/cli
Step 3: After installing angular cli, set up a new project in angular using the below command:
ng new my-angular-app
Folder structure:
Angular Folder structureDependencies:
"dependencies": {
"@angular/animations": "^17.1.0",
"@angular/common": "^17.1.0",
"@angular/compiler": "^17.1.0",
"@angular/core": "^17.1.0",
"@angular/forms": "^17.1.0",
"@angular/platform-browser": "^17.1.0",
"@angular/platform-browser-dynamic": "^17.1.0",
"@angular/router": "^17.1.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Step 3: Navigate to the project using cd your-project-name and open the terminal and write the command mentioned below to generate a new component in angular:
ng generate component component-name
"OR"
You can use this shortcut
ng g c component-name
Note: After this command, it will ask you for styling so chose as per your needs( css for now) then it will ask for SSR, press n to skip it then the component will be generated in src folder of the project.
The component will look like this:
New component in angularExample to create a New Component in Angular
Code: Now add the following code in the required files.
HTML <!-- Html Structure of component--> <div class="card"> <div class="card-header"> <h2 class="heading">GeeksforGeeks</h2> </div> <div class="card-body"> <img src="../../assets/img/gfg logo.png" alt="GeeksforGeeks Image" /> <p> GeeksforGeeks is a leading platform that provides computer science resources and coding challenges for programmers and technology enthusiasts, along with interview and exam preparations for upcoming aspirants. With a strong emphasis on enhancing coding skills and knowledge, it has become a trusted destination for over 12 million plus registered users worldwide. The platform offers a vast collection of tutorials, practice problems, interview tutorials, articles, and courses, covering various domains of computer science. </p> </div> </div>
HTML // app.compoenent.html <app-card></app-card>
CSS /* my-component.component.css */ body { font-family: 'Roboto', sans-serif; } .heading { text-align: center; color: #ffff; } .card { width: 20%; height: auto; margin: auto; margin-top: 10vh; border-radius: 10px; overflow: hidden; background: linear-gradient(to bottom right, #4e54c8, #8f94fb); } .card-header { background: linear-gradient(to top left, #00a300, #00c900); padding: 10px; } .card-body { padding: 20px; } .card img { max-width: 60%; height: auto; margin-bottom: 20px; border-radius: 20px; display: flex; margin: auto; } .card-body p { color: #ffff; margin-top: 20%; } @media screen and (max-width: 768px) { .card { width: 90%; height: auto; } }
JavaScript // app.component.ts import { Component, NgModule } from '@angular/core'; import { RouterOutlet } from '@angular/router'; import { CardComponent } from './my-component/my-component.component'; @Component({ selector: 'app-root', standalone: true, imports: [RouterOutlet, my - component], templateUrl: './app.component.html', styleUrl: './app.component.css' }) export class AppComponent { title = 'card'; }
JavaScript // my-component.component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-component', standalone: true, imports: [], templateUrl: './my-component.component.html', styleUrl: './my-component.component.css' }) export class my-component { }
Run the project using command given below:
ng serve
Output:
How do you create a new component in Angular Similar Reads
How to create Class Component in React?
JavaScript syntax extension permits HTML-like code within JavaScript files, commonly used in React for defining component structure. It simplifies DOM element manipulation by closely resembling HTML syntax. JSX facilitates the creation of reusable UI building blocks, defined as JavaScript functions
2 min read
How to Create a new module in Angular ?
Modules are most important when it comes to building strong and scalable Angular applications. They help organize your code, promote modularity, and improve maintainability. It encourages collaboration among developers by grouping related components, directives, pipes, and services. In this article,
3 min read
How to create components in ReactJS ?
Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
How to inject service in angular 6 component ?
Service is a special class in Angular that is primarily used for inter-component communication. It is a class having a narrow & well-defined purpose that should perform a specific task. The function, any value, or any feature which may application required, are encompassed by the Service. In oth
4 min read
How to create nested controllers in Angular.js ?
A controller in AngularJS is a JavaScript object created with the help of a JavaScript object constructor. A controller can contain properties and functions. Controllers are used for controlling the application data of an AngularJS application. In this article, we will see the nested controllers in
4 min read
How to create class in Angular Project?
In Angular, TypeScript is the primary language for writing code. One of the fundamental concepts in TypeScript is classes, which allow you to define blueprints for creating objects with properties and methods. In this article, we'll explore how to create classes in Angular projects and understand th
2 min read
How to generate Components in a specific folder with Angular CLI ?
Angular CLI is a command-line interface tool. Angular applications can be directly created, initialized, developed, and maintained using the command shell provided by CLI. Angular CLI facilitates to generate any number of components in the application, & by default, the Components are generated
3 min read
How to create a custom pipe in AngularJS ?
In this article, we will learn how to generate custom pipe other than using built-in pipe in Angular & also explore its implementation. The Pipe is a function that is used to modify the data before rendering it to the user. Some of the pre-built pipes are date, uppercase, lowercase, currency, de
6 min read
Explain the Role of @Component Decorator in Angular
Components are the fundamental building blocks that create the different parts of our Angular application that we see and interact with. They combine the appearance and functionality of a specific part of the application and can be used in different places..A decorator is a TypeScript feature that a
4 min read
Angular Material Toolbar Component
Angular Material is a UI component library which is developed by Google so that Angular developers can develop modern applications in a structured and responsive way. By making use of this library, we can greatly increase the user experience of an end-user thereby gaining popularity for our applicat
3 min read