在Angular 7中使用httpClient删除数组中的数组

时间:2018-12-10 20:38:41

标签: angular typescript

我有问题。 我有服务DataStorageServiceService,在此服务中,我有2种方法,deleteCategorydeleteProduct。我想从他的组件AdminComponent中删除服务器上的产品和类别。我服务器上的结构是:

{
                id: 1, name: 'Mr.', description: 'dasdad', image: "sada", products: [
                    { id: 0, name: 'Milos', description: "sdada", numberOfProduct: 1, image: 'ssad' },
                ]



            },
            {
                id: 2, name: 'Misko', description: 'dasdad', image: "sada", products: [
                    { id: 0, name: 'Milos', description: "sdada", numberOfProduct: 1, image: 'ssad' },
                    { id: 1, name: 'Somi', description: "haha", numberOfProduct: 1, image: 'haha' }
                ]
            }

我的删除deleteCategory方法可以正常工作,但是我的deleteProduct现在可以很好地工作,并且不删除服务器上的产品。

这是我的deleteCategory方法:

 deleteCategory(product: CategoryModel | number): Observable<CategoryModel> {
    const id = typeof product === 'number' ? product : product.id;
    const url = `${this.cateogryUrl}/${id}`;

    return this.http.delete<CategoryModel>(url, httpOptions).pipe(

      catchError(this.handleError<CategoryModel>('deleteProduct'))
    );
  }

这是我的deleteProduct方法:

 deleteProduct(product: ProductModel | number, index:number): Observable<ProductModel> {
    const idCategory = typeof product === 'number' ? product : product.id;
    const id = index;

const url = `${this.cateogryUrl}/${id}/${idCategory}`;

    return this.http.delete<ProductModel>(url, httpOptions).pipe(

      catchError(this.handleError<ProductModel>('deleteProduct'))
    );


}

我删除组件的方法是:

deteleProduct(indexCategory: number, indexProduct: number, category: ProductModel) {
    this.dataStorageServiceService.deleteProduct(category,indexProduct).subscribe()
    this.categoryList[indexCategory].products.splice(indexProduct, 1);

  }

  deteleCategory(indexProduct: number, category: CategoryModel) {
    this.dataStorageServiceService.deleteCategory(category).subscribe();
    this.categoryList.splice(indexProduct, 1);

  }

有人知道为什么我不能删除服务器上的产品

0 个答案:

没有答案