1

I am trying to send two variable from child component to the parent component

I have placed the child selector to the parent component view , where I used child tag and bind the @output name myOutput to the getData function in the parent component and passed the event handler to it

<app-form-test [myInput]='myInputString' (myOutput)="getData($event)"></app-form-test>

parent ts file =>

  getData(value) {
    alert(value);
  }

child ts file

export class FormTestComponent implements OnInit {
  @Input() myInput: [];

  @Output() myOutput: EventEmitter<string> = new EventEmitter();
  firstOutputString = 'hello I am coming from child component'
  secondOutputString = " I am second string";

  ngOnInit() {

  }
  SendData() {
    this.myOutput.emit(this.firstOutputString);
  }
 }

Here I want to pass another variable secondOutputString to the parent component from child component I tried to pass it using

SendData() {
        this.myOutput.emit(this.firstOutputString , this.secondOutputString);
      }

But I am getting error

Anurag Ranjan
  • 242
  • 1
  • 4
  • 14

1 Answers1

0

in child .ts:

@Output() myOutput: EventEmitter<{firstOutputString: string, secondOutputString: string}> = new EventEmitter();

SendData() {
    this.myOutput.emit({
      firstOutputString: this.firstOutputString,
      secondOutputString: this.secondOutputString
    });
}

and in parent .ts:

getData(value) {
    console.log(value.firstOutputString);
    console.log(value.secondOutputString);
}
danday74
  • 52,471
  • 49
  • 232
  • 283