Recently, I decided to delve into Angular and experiment with some hands-on projects. One of the challenges I encountered was utilizing the same component for both editing and creating a product. To address this, I implemented an if
statement within the component to differentiate between the two actions. However, I noticed that the code inside the if
statement wasn't being executed when the request URL was
http://localhost:4200/product/Edit/Redmond?edit=true
. Although I had included code to manage the route parameters (which I omitted for conciseness), I couldn't determine what exactly was missing from my implementation.
export class ProductEditComponent implements OnInit {
isEdit= false;
uiProduct:ProductModel;
ngOnInit() {
this.activatedRoute.queryParams.subscribe(queryParams=>{
this.isEdit=queryParams['edit']
console.log(this.isEdit); // it's true here for the Edit Route
})
if (this.isEdit ==true) {
this.productService.
GetProductByName(this.name)
.subscribe((response: ProductModel) => {
this.uiProduct = response;
});
}
}