angular 2 @input 的数据绑定模板

angular 2 data binding template for @input

我很想知道你们如何转换绑定来自

的数据输入
template: `<guage-bar [guage]=guageBar></guage-bar>`

做这样的事情

let data = new PercentValue(20, 100); 
let guageBar = new GuageBar(this.data,'Database Health');

GuageBarComponent.guage = this.guageBar;

我试过了,没用。我的最终目标是尝试使用服务在另一个 class 中输入 属性 值。

谢谢!

可以使用@ViewChild and use the lifecycle hook ngAfterViewInit设置值

import { ViewChild, AfterViewInit } from '@angular/core'

@Component({
  template: `<guage-bar></guage-bar>`
})
class ParentComponent implements AfterViewInit {

  @ViewChild(GuageBarComponent) guageBarCmp: GuageBarComponent;

  // the child component is available when this is called.
  // before that it will be null
  ngAfterViewInit() {
    guageBarCmp.guageBar = guageBar;
  }
}