I have enums on back- and front-end side with category names:
export enum CategoryEnum {
All = 'All',
Category1 = 'Category1',
Category2 = 'Category2',
Category3 = 'Category3',
Category4 = 'Category4'
}
I am displaying them in this way (shop.component.html - this is after many trials):
<h5 class="text-warning ml-3">Categories</h5>
<ul class="list-group my-3">
<li class="list-group-item"
*ngFor="let category of categories | keyvalue"
[class.active]="category.value === this.categorySelected"
[value]="category.value"
(click)="onCategorySelected(category.value)">
{{category.value}}
</li>
</ul>
This is my shop.service.ts:
@Injectable({
providedIn: 'root'
})
export class ShopService {
baseUrl = 'https://localhost:5001/api/';
constructor(private http: HttpClient) { }
getProducts(category?: string) {
let params = new HttpParams();
if (category) {
params.append('category==', category);
//params.append('?Filters=category%3D%3D', category);
}
return this.http.get<IProduct[]>(this.baseUrl + 'products', {observe: 'response', params})
.pipe(
map(response => {
return response.body
})
);
}
}
In Swagger, this query shows correct results ("%3D" equals "=" so "category%3D%3D4" equals "category==4"): Curl curl -X GET "https://localhost:5001/api/Products?Filters=category%3D%3D4" -H "accept: /" Request URL https://localhost:5001/api/Products?Filters=category%3D%3D4
After many trials, I cannot fix that after click on some category displaying corresponding products on front-end. shop.componet.ts:
@Component({
selector: 'app-shop',
templateUrl: './shop.component.html',
styleUrls: ['./shop.component.scss']
})
export class ShopComponent implements OnInit {
products: IProduct[];
categories = Object.keys(CategoryEnum);
categorySelected: string;
constructor(private shopService: ShopService) { }
ngOnInit(): void {
this.getProducts();
}
getProducts() {
this.shopService.getProducts(this.categorySelected).subscribe((response: any) => {
this.products = response.data.$values;
}, error => {
console.log(error);
});
}
onCategorySelected(categoryName: string) {
this.categorySelected = categoryName;
this.getProducts();
}
}
If it possible please provide short explanation also.
Edit: I tried many different paths after changes advised by @traynor:
getProducts(category?: string) {
let params = new HttpParams();
if(category) {
// params.append('category==', category.toString());
// params.append('category=', category.toString());
// params.append('category==', category);
// params.append('category=', category);
// params.append('filters=category==', category.toString());
// params.append('filters=category=', category.toString());
// params.append('filters=category==', category);
// params.append('filters=category=', category);
// params.append('Filters=category', category);
// params.append('Filters=category', category.toString());
// params.append('Filters=category=', category);
// params.append('Filters=category=', category.toString());
// params.append('Filters=category==', category);
// params.append('Filters=category==', category.toString());
// params.append('?Filters=category==', category);
// params.append('?Filters=category==', category.toString());
// params.append('?Filters=category=', category);
// params.append('?Filters=category=', category.toString());
// params.append('?Filters=category', category);
// params.append('?Filters=category', category.toString());
// params.append('?Filters=category%3D%3D', category);
// params.append('?Filters=category%3D%3D', category.toString());
// params.append('?Filters=category%3D', category);
// params.append('?Filters=category%3D', category.toString());
params.append('?Filters=category', category);
// params.append('?Filters=category', category.toString());
}
But still doesn't work.
Edit 2: After applying another changes from @traynor I saw that he was right about that HttpParams is immutable. Also I needed to correct the route:
if (category) {
params = new HttpParams().append('Filters=category=', category);
}
Huge thanks one more time I am beginner with front-end and not yet working as developer and this is my portfolio project for future work :)