How to make reactive variables between COMPONENTS? Angular 2/4

0

Good Stackers, Today I propose a doubt that I can not find or identify.

Lately I'm practicing with the Inputs / Outputs and this has led me to make this example:

In component A, I have @Output() outputVariableA = [hola, hola2, hola3]

In component B, I have @Input() inputVariableB [];

In component B, I define a function that modifies the inputVariableB .

How do I make it REAGENT, between the two VARIABLES the modification.

  

If you have not understood, ask me.

Thank you very much for helping!

    
asked by PictorGames 13.08.2017 в 13:59
source

1 answer

0

You can choose in component B to make your variable "Observable" so that you can subscribe to the changes that occur in it from both component A and B.

export class BComponent {

  inputVariableBChange = new BehaviorSubject<number[]>([]);
  get inputVariableB(): number[] { return this.inputVariableBChange.value; }
  @Input() set inputVariableB(values: number[]) { return this.inputVariableBChange.next(values); }

  constructor() {
    this.inputVariableBChange.subscribe((values: number[]) => {
      // Aquí tienes los nuevos valores de tu inputVariableB.
    })
  }

  tuFuncion(): void {
    // Aquí modificas (notificas/emites) cambios en tu inputVariableB.
    const values = []; // Nuevos valores...
    this.inputVariableBChange.next(values);
  }

}
    
answered by 13.08.2017 в 14:44