Two way binding between components in angular 2-4

Bellash picture Bellash · Nov 2, 2017 · Viewed 11.4k times · Source

I would like the child component value to be bound to the parent component. How to accomplish this when @Input() and [(ngModel)] are not enough ?

here is a plunker

Answer

Vivek Doshi picture Vivek Doshi · Nov 2, 2017

Here you have to set @Output also and change the component like

export class CounterComponent {

  @Output('initoChange') emitter: EventEmitter<string> = new EventEmitter<string>();
  @Input('inito') set setInitoValue(value) {
      this.count = value;
  }
  count: number = 0;

  increment() {
    this.count++;
    this.emitter.emit(this.count);
  }

  decrement() {
    this.count--;
    this.emitter.emit(this.count);
  }

}

Here is the link to plunker , please have a look.