首页 > 解决方案 > 如何以编程方式更改 @Output() 上的 bindingPropertyName

问题描述

我需要以编程方式更改一个 @Output() 上的 bindingPropertyName。

我使用一个指令在一个聚合物 2.0 组件中的属性(将 reflectToAttribute 设置为 true)和我在 Angular 5 中的应用程序之间进行两种方式绑定。

代码:

@Directive({
  selector: "[bindPolymer]"
})
export class BindPolymerDirective {

  @Output("valueChange") change: EventEmitter<any> = new EventEmitter();

  @HostListener("value-changed", ["$event.target.value"]) onInputChange(value) {
    if (value) {
      this.change.emit(value);
    }
  }
}
<polymer-input [(value)]="user" bindPolymer></polymer-input>

在这种情况下,需要双向绑定的属性是值,所以它就像一个魅力。但在其他情况下,他可能会有所不同。

任何人都可以告诉我如何实现这一目标?或者也许给我另一种解决问题的方法?谢谢

标签: angularpolymer

解决方案


在 Javascript 中,对象是引用。

所以简单地做

@Output("valueChange") change: EventEmitter<any> = new EventEmitter();
@Output("valueChangeBis") changeBis = this.change;

应该做的伎俩


推荐阅读