【问题标题】:Taking two variable from parent component to child component将两个变量从父组件传递到子组件
【发布时间】:2019-02-24 10:54:18
【问题描述】:

我正在尝试将两个变量从子组件发送到父组件

我已将子选择器放置到父组件视图,在这里我使用子标记并将@output 名称 myOutput 绑定到父组件中的 getData 函数并将事件处理程序传递给它

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

父 ts 文件 =>

  getData(value) {
    alert(value);
  }

子 ts 文件

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);
  }
 }

在这里,我想将另一个变量 secondOutputString 从子组件传递给父组件,我尝试使用

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

但是我遇到了错误

【问题讨论】:

标签: angular angularjs-directive


【解决方案1】:

在子 .ts 中:

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

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

在父 .ts 中:

getData(value) {
    console.log(value.firstOutputString);
    console.log(value.secondOutputString);
}

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 2020-07-23
    • 2020-02-02
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2019-01-21
    相关资源
    最近更新 更多