Problem with observables in angular2

1

Hello, how could I do the following, I have a ListComponent component that serves as a template. At the end I have ListComponent1 and ListComponent2, which is passed by input different options (type of menu if it is menuPrincipal, authentication, etc), in the 2 components I have in the constructor an observable linked to a redux state.

Constructor

   this.store.select('ListOpciones')
    .subscribe(data=>{
    if(data instanceof Array)
        this.options=data;
    }); 

The problem is the following, when the store captures a new data, this is replicated to the 2 components in which it was instantiated, it is obvious because the 2 handle the same observable, however I want to do the following for example if when I subscribe to the store in some way filter by type, and if for example the type is type: A, the changes only affect Component1, and if type: B affect Component2.

These are doing it in this way because the template component represents a menu of options, among these I have the navigation bar, the authentication menu, among other things, to which I bring your options by consulting an api through a service.

    
asked by Kevin AB 13.04.2017 в 23:08
source

1 answer

0

You can use the operator takeWhile or the operator filter .

this.store.select('ListOpciones')
  .takeWhile((e: any) => {
    return e instanceof Array
  }).subscribe(data => {
    //....
  });

this.store.select('ListOpciones')
  .filter((e: any) => {
    return e instanceof Array
  }).subscribe(data => {
    //....
  });
    
answered by 24.04.2017 в 17:37