How to use an Observable of an Observable object in Angular 18

I have a beginner Angular project and I want to load a component where I get the ID of a record from the routerparams and from that I do an api call to my backend to get the data.
The issue is that my return is Observable<Observable>
and I’m not sure how to use this to get the data from the Product with that ID.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Component, Input, OnInit } from '@angular/core';
import { Product, ProductsService } from './products.service';
import { map, Observable } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-product-view',
standalone: true,
imports: [CommonModule],
template: `
product-view works!
<br />
<article class="product-panel" *ngIf="(product$ | async) as product">
{{product.id}}
//ABOVE CODE THROWS ERROR
//NOTE I WANT TO GET PRODUCT DATA BUT IT IS OBSERVABLE<Product>
</article>
`,
styles: ``
})
export class ProductViewComponent implements OnInit{
constructor(private productsService: ProductsService, private route: ActivatedRoute){}
product$: Observable<Observable<Product>>;
productId: string;
ngOnInit(): void {
this.product$ = this.route.paramMap.pipe(
map(params => {
return this.productsService.getProduct(params.get('id')!);
})
)
}
}
</code>
<code>import { Component, Input, OnInit } from '@angular/core'; import { Product, ProductsService } from './products.service'; import { map, Observable } from 'rxjs'; import { ActivatedRoute } from '@angular/router'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-product-view', standalone: true, imports: [CommonModule], template: ` product-view works! <br /> <article class="product-panel" *ngIf="(product$ | async) as product"> {{product.id}} //ABOVE CODE THROWS ERROR //NOTE I WANT TO GET PRODUCT DATA BUT IT IS OBSERVABLE<Product> </article> `, styles: `` }) export class ProductViewComponent implements OnInit{ constructor(private productsService: ProductsService, private route: ActivatedRoute){} product$: Observable<Observable<Product>>; productId: string; ngOnInit(): void { this.product$ = this.route.paramMap.pipe( map(params => { return this.productsService.getProduct(params.get('id')!); }) ) } } </code>
import { Component, Input, OnInit } from '@angular/core';
import { Product, ProductsService } from './products.service';
import { map, Observable } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-product-view',
  standalone: true,
  imports: [CommonModule],
  template: `
    
      product-view works!
      <br />
      
    <article class="product-panel" *ngIf="(product$ | async) as product">
            {{product.id}}
             //ABOVE CODE THROWS ERROR
             //NOTE I WANT TO GET PRODUCT DATA BUT IT IS OBSERVABLE<Product>
    </article>
  `,
  styles: ``
})

export class ProductViewComponent implements OnInit{
  
  constructor(private productsService: ProductsService, private route: ActivatedRoute){}
  
  product$: Observable<Observable<Product>>;
  productId: string;

  ngOnInit(): void {
    this.product$ = this.route.paramMap.pipe(
      map(params => {
      return this.productsService.getProduct(params.get('id')!);
      })
    )
  }
}

Here is my service:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
export interface Product {
id: string;
name: string;
category: string;
subCategory: string;
}
@Injectable({
providedIn: 'root'
})
export class ProductsService {
readonly URL = "http://localhost:5999/products";
constructor(private http: HttpClient) {
this.getProducts();
}
getProducts(): Observable<Product[]> {
return this.http.get<Product[]>(this.URL);
}
getProduct(id:string): Observable<Product> {
return this.http.get<Product>(this.URL + "/" + id);
}
}
</code>
<code>import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; export interface Product { id: string; name: string; category: string; subCategory: string; } @Injectable({ providedIn: 'root' }) export class ProductsService { readonly URL = "http://localhost:5999/products"; constructor(private http: HttpClient) { this.getProducts(); } getProducts(): Observable<Product[]> { return this.http.get<Product[]>(this.URL); } getProduct(id:string): Observable<Product> { return this.http.get<Product>(this.URL + "/" + id); } } </code>
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

export interface Product {
  id: string;
  name: string;
  category: string;
  subCategory: string;
}

@Injectable({
  providedIn: 'root'
})
export class ProductsService {

  readonly URL = "http://localhost:5999/products";

  constructor(private http: HttpClient) { 
    this.getProducts();
  }
  getProducts(): Observable<Product[]> {
    return this.http.get<Product[]>(this.URL); 
  }
  getProduct(id:string): Observable<Product> {
    return this.http.get<Product>(this.URL + "/" + id);
  }
}

I could get the router param in the way shown below but I kind of like the way I set it up because if the id in the routeparams changes it will do another api call.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> this.productId = this.route.snapshot.params['id'];
this.product$ = this.productsService.getProduct(this.productId);
</code>
<code> this.productId = this.route.snapshot.params['id']; this.product$ = this.productsService.getProduct(this.productId); </code>
   this.productId = this.route.snapshot.params['id'];
   this.product$ = this.productsService.getProduct(this.productId);

(Note: for this method I would change the this.product$ to be of type Observable instead of Observable<Observable> )

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật