【问题标题】:ViewChildren for ng-templateng-template 的 ViewChildren
【发布时间】:2017-03-02 22:29:53
【问题描述】:

我有一个 angular2 组件,其模板如下所示:

<div>
    <div><ng-template #left></ng-template></div>
    <div><ng-template #right></ng-template></div>
</div>

我希望能够使用 ViewChildren 检索对所有 ng-template 元素的引用,但我不知道需要在括号之间传递的类型。

【问题讨论】:

    标签: angular


    【解决方案1】:
    @ViewChildren(TemplateRef) templates: QueryList<TemplateRef>;
    
    ngAfterViewInit() {
      console.log(this.templates.toArray());
    }
    

    @ViewChild('left') left:TemplateRef;
    @ViewChild('right') right:TemplateRef;
    
    ngAfterViewInit() {
      console.log(this.left, this.right);
    }
    

    【讨论】:

    • 嘿,我刚刚用ViewContainerRef写了an article on DOM manipulation,有时间可以看看。
    • visual studio 代码抱怨缺少泛型:[ts] Generic type 'TemplateRef&lt;C&gt;' requires 1 type argument(s) 我不知道,不应该是ElementRef 吗?
    • 我无法看到泛型参数将发挥什么作用。 TemplateRef&lt;Object&gt; 应该做恕我直言。
    • 泛型类型用于模板上下文对象。如果不使用上下文对象,只需使用any
    • @GünterZöchbauer 是否可以在模板被 ngTemplateOutlet 盖章时查询模板?
    猜你喜欢
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多