Answer the question
In order to leave comments, you need to log in
Can't view an individual product?
I started to study Angular, did the first tutorial.
If I click on a product to view it, I don't have the id
product.ts
export const products = [
{
name: 'Phone XL',
price: 799,
description: 'A large phone with one of the best screens'
},
{
name: 'Phone Mini',
price: 699,
description: 'A great phone with one of the best cameras'
},
{
name: 'Phone Standard',
price: 299,
description: ''
}
];
<h2>Products</h2>
<div *ngFor="let product of products">
<h3>
<a [title]="product.name + productId+ ' details'" [routerLink]="['/products', productId]">
{{ product.name }}
</a>
</h3>
<p *ngIf="product.description">
{{product.description}}
</p>
<button (click)="share()">
Share
</button>
<app-product-alerts [product]="product" (notify)="onNotify()">
</app-product-alerts>
</div>
import { Component } from '@angular/core';
import { products } from '../products';
@Component({
selector: 'app-product-list',
templateUrl: './product-list.component.html',
styleUrls: ['./product-list.component.css']
})
export class ProductListComponent {
products = products;
share() {
window.alert('The product has been shared!');
}
onNotify() {
window.alert('You will be notified when the product goes on sale');
}
}
import { Component, OnInit } from '@angular/core';
import { from } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { products } from '../products';
import { CartService } from '../cart.service';
@Component({
selector: 'app-product-details',
templateUrl: './product-details.component.html',
styleUrls: ['./product-details.component.css']
})
export class ProductDetailsComponent implements OnInit {
product;
// @Input() product: product;
constructor(
private route: ActivatedRoute,
private cartService: CartService
) {}
addToCart(product) {
this.cartService.addToCart(product);
window.alert('Your product has been added to the cart!');
}
ngOnInit() {
this.route.paramMap.subscribe(params => {
this.product = products[+params.get('productId')];
});
}
}
<h2>Product Details</h2>
<div *ngIf="product">
<h3>{{ product.name }}</h3>
<h4>{{ product.price | currency }}</h4>
<p>{{ product.description }}</p>
<button (click)="addToCart(product)">Buy</button>
</div>
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { TopBarComponent } from './top-bar/top-bar.component';
import { ProductListComponent } from './product-list/product-list.component';
import { ProductAlertsComponent } from './product-alerts/product-alerts.component';
import { ProductDetailsComponent } from './product-details/product-details.component';
import { CartService } from './cart.service';
import { CartComponent } from './cart/cart.component';
@NgModule({
imports: [
BrowserModule,
ReactiveFormsModule,
RouterModule.forRoot([
{ path: '', component: ProductListComponent },
{
path: 'products/:productId',
component: ProductDetailsComponent
},
{ path: 'cart', component: CartComponent }
])
],
declarations: [
AppComponent,
TopBarComponent,
ProductListComponent,
ProductAlertsComponent,
ProductDetailsComponent,
CartComponent
],
bootstrap: [AppComponent],
providers: [CartService]
})
export class AppModule {}
Answer the question
In order to leave comments, you need to log in
used in product-list.component.html to build a link to product details productId where should it come from? where is it announced?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question