1

在我的 Angular 2 应用程序中,我有一个组件(mainComp),其中包括另一个组件

 <my-comp></my-comp>

my-comp发出(在选择/单击时)它的值(它是一个自定义下拉列表)通过

this.optionSelected.emit(currentOption == "Not described" ? null : currentOption);

mainComp接收下拉列表的值

4

1 回答 1

3

你有一个 2-way-binding,所以如果对象发生变化,结果也应该被刷新。如果您有 EventEmitter,请在构造函数中订阅,例如:

class MainComp {
  let value;
  ...
  constructor(private myComp : MyComp) {
    myComp.optionSelected.subscribe{
      (value) => this. value = value;
    }
}

现在,每次 EventEmitter 触发发射时,MainComp 中的值属性都会更新。

如果您想要一些有趣的东西,请查看 ngOnChanges ( https://angular.io/docs/ts/latest/cookbook/component-communication.html )

更新:你的意思是什么。像这样:

class MainComp {
  template: `<myComp [(value)]=value></myComp>`
  ...
}

class MyComp {
  @Input(): value;
  ...
}

这应该创建一个 2-way-binding,所以如果一个组件编辑值,另一个会得到通知。看看这个:https ://angular.io/docs/ts/latest/guide/template-syntax.html#!#ngModel

于 2016-06-27T09:53:34.990 回答