[英]angular passing input to child components in ng-content
我想使用內容投影將一些屬性從父組件傳遞給子組件,這可能嗎?
例如,這是我的模板:
<my-form [display]="'horiz'">
Email: <my-input [type]="'email'" ...></my-input>
Name: <my-input [type]="'name'" [display]="'vert'" ...></my-input>
...
</my-form>
現在my-form
組件有這樣的模板:
<form ...>
<ng-content></ng-content>
</form>
我想要的是my-form
的display
屬性可以通過my-input
組件訪問,這樣它也可以被my-input
組件覆蓋,就像它對於Name輸入一樣。
那可能嗎?
您可以使用@ContentChildren
,在stackblitz上做一個例子:
https://stackblitz.com/edit/angular-content-children-example
我已使用ContentChildren
的組件引用修改了ngAfterViewInit
的值。
希望能幫助到你。 如果有更好的方法,請隨時更新代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.