我如何在另一个服务提供商中调用服务提供商 - angularjs2

How can I call a service provider within another one - angularjs2

我有一些产品和购物车清单。这是循环购物车列表以使用 id

获取特定产品的函数
getCartList(){
        this.cart = CART;
        this.cart.forEach((cart: Cart) => {
            let id = parseInt(cart.productid.trim());
              //**this.product = this._productService.getProduct(id); => Here I need to call another service.**
            });
        });
        console.log(this.cart);
    }

只需在引导您的应用程序时定义此服务并将其注入您要使用它的服务中即可:

bootstrap(AppComponent, [ CardService , ProductService ]);

服务中:

@Injectable()
export class CardService {
  constructor(private _productService: ProductService) {
  }
}

不要忘记指定 @Injectable 装饰器。

这个问题可以提供更多关于依赖注入、分层注入器和服务注入如何工作的细节:

  • What's the best way to inject one service into another in angular 2 (Beta)?