Jao Ming
Jao Ming

Reputation: 19

Rxjs Subject/Observable subscribe Array of list

I have one easy question. I have service DataStorageServiceService and in this service I have shopCartProtudct = new Subject<ProductModel[]>();. I have component SingleProductComponent and from this component I want sent data sent data in to shopCartProtudct but like array, I want to interact with interact shopCartProtudct like basic array, than means, I want to store data and delete. This is posible?

Upvotes: 1

Views: 8597

Answers (1)

Aragorn
Aragorn

Reputation: 5289

You are likely looking for a Service implementation like this: Note: Untested code, consider it a pseudo code almost.

export class DataStorageService {

    //A subject that you can subscribe on
    shopCardProduct = new Subject<ProductModel[]>();

    //array behind the products in the cart
    products : ProductModel[] = new Array<ProductModel>() ;

    constructor(){
        this.shopCardProduct = new BehaviorSubject<ProductModel>(new Array<ProductModel>())
    }

    shop(product: ProductModel){
        this.products.push(product);
        this.shopCardProduct.next(this.products);
    }

    getCart(): Subject<ProductModel[]> {
        return this.shopCardProduct;
    }

}

Upvotes: 2

Related Questions