Angular2: ng-content attributes passing to child component

心已入冬 提交于 2019-12-04 02:48:24

It is possible to pass variable to projected content (assuming component cjc-box declares property focus and component cjc-input declares property hasfocus):

<div cjc-box #box><div cjc-input [hasfocus]="box.focus"></div></div>

This is one-way binding, if you want two-way it is slightly more complex:

  • Add @Input() decorator to focus property of box component.
  • Add @Input() decorator to hasfocus property of input component
  • Add @Output() hasfocusChange:EventEmitter<any> = new EventEmitter<any>(); to input component.
  • Add this.hasfocusChange.emit(this.hasfocus); after hasfocus change in your input component.
  • Change template to <div cjc-box #box><div cjc-input [(hasfocus)]="box.focus"></div></div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!