nip*_*777 5 angular-decorator angular
我正在尝试使用标签@ContentChildren来拾取所有物品#buttonItem.
@ContentChildren('buttonItem', { descendants: true })
Run Code Online (Sandbox Code Playgroud)
当我们直接在父组件中包含ref项时,这是有效的.
<!-- @ContentChildren returns child item -->
<parent-component>
<button #buttonItem></button>
<parent-component>
Run Code Online (Sandbox Code Playgroud)
但是,如果带有#buttonItemref 的元素包含在自定义组件中,@ContentChildren即使我设置了该{descendants: true}选项,也不会被选中.
<!-- @ContentChildren returns empty -->
<parent-component>
<child-component-with-button-ref></child-component-with-button-ref>
<parent-component>
Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的StackBlitz示例来演示这一点.
似乎没有通过 github 解决此项目的时间表...我还发现了一条评论,指出您无法跨 ng-content 边界进行查询。
https://github.com/angular/angular/issues/14320#issuecomment-278228336
下面是使元素从OptionPickerComponent.
在那里OptionPickerComponent计数#listItem并发出数组AfterContentInit
@Output() grandchildElements = new EventEmitter();
@ViewChildren('listItem') _items
ngAfterContentInit() {
setTimeout(() => {
this.grandchildElements.emit(this._items)
})
}
Run Code Online (Sandbox Code Playgroud)
设置模板引用#picker,注册(grandchildElements)事件并将其设置$event为picker.grandchildElements
<app-option-picker #picker [optionList]="[1, 2, 3]" (grandchildElements)="picker.grandchildElements = $event" popup-content>
Run Code Online (Sandbox Code Playgroud)
创建输入以PopupComponent接受来自的值picker.grandchildElements
@Input('grandchildElements') grandchildElements: any
Run Code Online (Sandbox Code Playgroud)
接受输入app.component.htmlpicker.grandchildElements
<app-popup [grandchildElements]="picker.grandchildElements">
Run Code Online (Sandbox Code Playgroud)
popup.component设置console.log为打开和关闭
open() {
if (this.grandchildElements) {
console.log(this.grandchildElements);
}
else {
console.log(this.childItems);
}
close() {
if (this.grandchildElements) {
console.log(this.grandchildElements);
}
else {
console.log(this.childItems);
}
Run Code Online (Sandbox Code Playgroud)
popup.component把你的ContentChildren背改成listItem
@ContentChildren('listItem', { descendants: true }) childItems: Element;
Run Code Online (Sandbox Code Playgroud)
popup.component.html设置标题表达式
<h3>Child Items: {{grandchildElements ? grandchildElements.length : childItems.length}}</h3>
Run Code Online (Sandbox Code Playgroud)
斯塔克闪电战
| 归档时间: |
|
| 查看次数: |
340 次 |
| 最近记录: |