I have an angular service that consumes rest api.
@Injectable({
providedIn: 'root'
})
export class ProductService {
private url: string = "/app/products";
constructor(private http: HttpClient) {
}
get(caregoryId: string) {
return this.http.get<Product[]>(`${this.url}`)
.pipe(map(products => products.filter(p => p.caregoryId == caregoryId)))
}
add(p: Product) {
return this.http.post<Product>(this.url, p);
}
}
And my component is:
export class ProductComponent implements OnInit{
items: Observable<Array<Product>>;
ngOnInit() {
this.route.params.subscribe(
(params: any) => {
this.items = this.productService.get(params.id);
})
}
addWidget() {
let item: any = { name: "p-1", color: "red" } }
this.productService.add(item))
/// how to add created item in my items array ?
}
}
I can get list of products and list them using async pipe in html. But I could not add the created item in my observable array. How can I do it?
subscribeinthis.productService.add(item).