【发布时间】:2016-06-04 20:08:49
【问题描述】:
我有一个名为 button-search 的组件,它有一个带有搜索选项的下拉菜单:
<button-search>
<item type="identifier">Identifier</item>
<item type="title">Title</item>
<item type="city">City</item>
<item type="town">Town</item>
<item type="address">Address</item>
<item type="postal">Postal</item>
<item type="divider"></item>
<item type="clear">Clear Search</item>
</button-search>
item 组件不应该直接渲染任何东西,而是将复杂的参数传递给button-search 组件,以便button-search 组件可以按照应该呈现的方式呈现这些下拉项。
Item定义如下:
@Component(
selector: 'item',
template: '')
class Item {
@Input() String type = "type-goes-here";
}
ButtonSearch定义如下:
@Component(
selector: 'button-search',
directives: const[Item],
template: '''
... enormous template ...
''')
class ButtonSearch {
ButtonSearch(@ViewChildren(Item) QueryList<Item> items){
print(items);
}
}
我看到的不是打印到控制台的 9 个项目的列表,而是[]。
我尝试使用字符串参数而不是对象,但它仍然给我 null。
ButtonSearch(@ViewChildren('item') QueryList<Item> items){
我缺少什么让
@ViewChildren获取所有项目并打印除[]之外的其他内容是否需要做一些特别的事情来获取
<item>和</item>之间的文本,或者@ViewChild是否会为此工作?
更新:更改模板以包含<ng-content></ng-content>:
template: '''
<ng-content></ng-content>
... enormous template ...
''')
我可以看到以下内容作为button-search 组件的一部分在浏览器中打印:
标识符、标题、城市、城镇、地址、邮政、清除搜索
所以至少我知道我所在的页面在其button-search 组件中确实有项目。
【问题讨论】:
-
请检查
Item组件是否被实例化。如果元素没有升级到实际的 Angular 组件,则无法查询Item。
标签: angular dart angular-dart