Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • AngularJS Tutorial
  • AngularJS Directives
  • AngularJS Functions
  • AngularJS Filters
  • AngularJS Examples
  • AngularJS Interview Questions
  • Angular ngx Bootstrap
  • AngularJS Cheat Sheet
  • AngularJS PrimeNG
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to create components in ReactJS ?
Next article icon

How to create a new component in Angular?

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

angularApp
Angular Folder structure

Dependencies:

"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:

newComponentFolderStructure
New component in angular

Example 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-to-generate-a-component-in-angular
How do you create a new component in Angular

Next Article
How to create components in ReactJS ?

J

jhrowym65
Improve
Article Tags :
  • Web Technologies
  • AngularJS
  • AngularJS-Basics

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences