2016-09-16 47 views
1

我知道我可以使用@ViewChildren注释来访问DOM元素。但是,在这种特殊情况下,entries属性在祖先组件中定义(MenuComp)。于是,我就用queries属性,如下所示:dart:通过查询替换ViewChildren

@Component(
    selector: 'my-component', 
    template: ''' 
    <ul> 
     <subentry-comp #entries *ngFor="let item of items" [item]="item"></subentry-comp> 
    </ul>''', 
    inputs: const <String>['items'], 
    queries: const <String, dynamic>{'entries': const ViewChildren('entries')}) 
class SubmenuComp extends MenuComp {} 

entries仍然是空。我是否以错误的方式使用queries属性?

+0

从未见过前面提到的'quieries' ind'@Component()'。我知道构造函数参数的'@Query()'注释,但只要我使用Angular2(自alpha后期以来),它就被弃用了。您的查询似乎查询模板中的元素。我不明白这与“祖先”有什么关系 –

+1

AFAIK @ @ @ ViewChild(ren)'和@ @ContentChild(ren)'建立在@ Query之上,我不希望'@ Query'做有很大的不同。 –

回答

1

从角度4.0.0-alpha + 1开始,如果在父类的字段上使用@ViewChildren,它将继承父级的注释。 Componentqueries参数将会消失。