Ecommerce App with Angular from scratch
If you’re a beginner looking to build an ecommerce app using Angular, you’re in the right place! In this tutorial, we will guide you through the process of creating an ecommerce app from scratch using Angular.
What is Angular?
Angular is a popular JavaScript framework that is used to build large-scale, high-performance web applications. It is backed by Google and offers excellent tools and features for developing web applications. Angular follows the component-based architecture, allowing developers to build modular and reusable code.
Setting up the Project
To get started, make sure you have Node.js installed on your machine. Node.js comes bundled with npm, a package manager that we will use to install Angular CLI (Command Line Interface).
- Open your command prompt or terminal
- Run the following command to install Angular CLI globally:
npm install -g @angular/cli
- Once the installation is complete, create a new Angular project using the following command:
ng new ecommerce-app
- Navigate to the project folder:
cd ecommerce-app
- Finally, start the development server:
ng serve
Creating Components
Now that we have our Angular project set up, let’s start creating the necessary components for our ecommerce app.
- Open your project in a text editor
- Create a new folder called “components” inside the “src” folder
- Inside the “components” folder, create a new component called “product-list” using the following command:
ng generate component product-list
- Similarly, create another component called “product-details”:
ng generate component product-details
Routing
Next, we need to set up routing for our Angular app. Open the “app-routing.module.ts” file located inside the “src” folder and add the following code:
“`typescript
import { NgModule } from ‘@angular/core’;
import { RouterModule, Routes } from ‘@angular/router’;
import { ProductListComponent } from ‘./components/product-list/product-list.component’;
import { ProductDetailsComponent } from ‘./components/product-details/product-details.component’;
const routes: Routes = [
{ path: ”, redirectTo: ‘products’, pathMatch: ‘full’ },
{ path: ‘products’, component: ProductListComponent },
{ path: ‘products/:id’, component: ProductDetailsComponent },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
“`
This code sets up the routes for our app. We have defined two routes: one for the product list page and another for the product details page. The `redirectTo` route will redirect the user to the product list page when they visit the root URL of our app.
Creating Services
Services are used to handle data and perform business logic in Angular. Let’s create a service to fetch product data from a server.
- Inside the “src” folder, create a new folder called “services”
- Inside the “services” folder, create a new file called “product.service.ts”
- Add the following code to the “product.service.ts” file:
“`typescript
import { Injectable } from ‘@angular/core’;
import { HttpClient } from ‘@angular/common/http’;
@Injectable({
providedIn: ‘root’
})
export class ProductService {
private apiUrl = ‘https://api.example.com/products’;
constructor(private http: HttpClient) { }
getProducts() {
return this.http.get(this.apiUrl);
}
getProductById(productId: string) {
return this.http.get(`${this.apiUrl}/${productId}`);
}
}
“`
This service uses Angular’s `HttpClient` to make HTTP requests to the server. Modify the `apiUrl` variable with the actual URL of your API that provides product data.
Rendering Data in Components
Now, let’s render the product data in our components.
- Open the “product-list.component.html” file located inside the “src/app/components/product-list” folder
- Replace the existing code with the following:
“`html
{{ product.name }}
Price: {{ product.price }}
“`
This code uses Angular’s `ngFor` directive to loop over the `products` array and display each product’s name and price. The `goToProductDetails` function is invoked when the user clicks the “View Details” button.
Fetching Data from the Server
To fetch product data from the server, we need to make use of the `ProductService` we created earlier. Let’s modify the “product-list.component.ts” file located inside the “src/app/components/product-list” folder:
“`typescript
import { Component, OnInit } from ‘@angular/core’;
import { ProductService } from ‘../../services/product.service’;
@Component({
selector: ‘app-product-list’,
templateUrl: ‘./product-list.component.html’,
styleUrls: [‘./product-list.component.css’]
})
export class ProductListComponent implements OnInit {
products: any[];
constructor(private productService: ProductService) { }
ngOnInit(): void {
this.productService.getProducts().subscribe(
(data: any[]) => {
this.products = data;
},
(error) => {
console.error(error);
}
);
}
goToProductDetails(productId: string) {
// Logic to navigate to the product details page goes here
}
}
“`
This code calls the `getProducts` function of the `ProductService` and subscribes to the returned Observable. When the data is received from the server, it is assigned to the `products` array.
Conclusion
We have successfully created an ecommerce app with Angular from scratch. In this tutorial, we covered the basics of Angular, set up a new project, created components, set up routing, created services to fetch data from the server, and rendered the data in our components. This is just the beginning, and you can continue building more features and functionality for your app.
Happy coding!
where to find nav var code
Sir I'm unable to access API due to domain issue
Sir project is nice can u send whole code for me
Hi, do you have the tutorial on how you created the HTML page?
Not Good .Even there is no proper code in the repo and also none of the api links are provided
Thanks alot it was great so much take away i hope we can get the continuation
Hello sir, API Keys are not working how can we do without API keys. can you help me with the API Keys
now in that api there is no data why…..?
Hello sir, do you have any project in angular node js mongodb and loopback?
Currently the api call is returning an empty array… no product! did you chenge anything in the api?? what to do now?
Hello Sir, you did a great job, it was great learning for me. it will be great if API also been shared
your code api links is not working . give me same another links
I'm getting cart.length error
Sir, can u share the html css code that u used at starting, because i wanna do that with u, not copy from u. And thx for this video, see u
how to fix cors Error
Bro can u share the html css code that u used at starting, because i wanna do that with u, not copy from u. And thx for this video, see u
Awesome ❤
where did you download the navbar from in bootstrap?
am getting trouble getting it
could you share the API code too.. because without it.. i cant see the products images… So Kindly share the api of products api thank You sir… and the explanation was excellent… I can easily understand the concepts… thank You 🥰🥰🥰🥰
thank you very much !