【问题标题】:How do i access ng-content?我如何访问 ng-content?
【发布时间】:2017-07-28 01:50:20
【问题描述】:

我想访问 ng-content 的内容,一开始只是为了获取内容元素的数量,但以后会更棒。

例如:

@Component({
selector: 'my-container',
template: `
    <div class="my-container">
        <div class="my-container-nav"><a class="my-container-previous"></a></div>
        <div class="my-container-body">
            <div class="my-container-items">
                <ng-content></ng-content>
            </div>
        </div>
        <div class="my-container-nav"><a class="my-container-next"></a></div>
    </div>  
`
})
export class MyContainer implements AfterContentInit {
    @ContentChildren(???) containerItems : QueryList<???>;
    ngAfterContentInit() {
        console.log('after content init: ' + this.containerItems.length);
    }
}

我将什么作为内容子项的类型?

它们可以是任何东西。我尝试了 ElementRef,但没有奏效,而且总是计数为零。

使用示例:

<my-container>
    <div>some content here</div>
    <another-component [title]="'another component there'"></another-component>
    <div>there's lots of content everywhere</div>
    <span>no assumptions would be fair</span>
</my-container>

我希望看到“内容初始化后:4”。

【问题讨论】:

  • 是您控制的子 Angular2 组件(即您想访问组件实例)吗?还是只是纯 HTML?
  • Sjoerd,可能是混合的。我期望随机 html,但我也期望组件。当它们是组件时,它们将具有不同的类型。

标签: angular


【解决方案1】:

我误解了@ContentChildren。它并不代表所有内容。它代表 ng-content 中的@Components。

直接回答问题:

如果我不提前知道它们的类型,我将如何在 ng-content 中获取 @Components 仍不清楚。但我现在意识到这有点傻:如果我不知道它们是什么,我想我到底该怎么处理它们?

间接回答问题:

要访问 ng-content 的所有内容,我可以将模板变量 (#items) 引入 ng-content 的父级,并通过 @ViewChild 作为 elementRef 引用它。那么孩子可以这样算:

@Component({
selector: 'my-container',
template: `
    <div class="my-container">
        <div class="my-container-nav"><a class="my-container-previous"></a></div>
        <div class="my-container-body">
            <div #items class="my-container-items">
                <ng-content></ng-content>
            </div>
        </div>
        <div class="my-container-nav"><a class="my-container-next"></a></div>
    </div>  
`
})
export class MyContainer implements OnInit {
    @ViewChild('items') items: ElementRef;
    ngOnInit() {
        console.log('count is: ' + this.items.nativeElement.children.length);
    }
}

对于其他面临类似困惑的人,我可以进一步扩展 @ContentChildren。假设我有 2 个@Components:component-a 和 component-b,我知道消费者会将它们放在我的组件的内容中:

<my-component>
    <component-a></component-a>
    <div>random html</div>
    <component-b></component-b>
    <component-a></component-a>
</my-component>

然后在我的组件中,我可以获取对各个组件类型的所有实例的引用:

export class MyContainer implements AfterContentInit {
    @ContentChildren(ComponentA) componentAInstances: QueryList<ComponentA>;
    @ContentChildren(ComponentB) componentBInstances: QueryList<ComponentB>;
    ngAfterContentInit() {
        console.log('count of component A: ' + this.componentAInstances.length);
        console.log('count of component B: ' + this.componentBInstances.length);
    }
}

控制台会显示有 2 个组件 A 和 1 个组件 B。

我应该指出,这是在候选版本 4 中。

【讨论】:

  • 添加到@ContentChildren 部分:您的组件可能共享一个公共基类。然后,您可以根据其基本类型选择组件,并在组件实例上调用基本类型中的任何方法。
  • 您需要正确设置提供程序以使@ContentChildren() 与基类一起工作。默认情况下这不起作用。通过添加providers: [{provide: BaseClass, useClass: SubClass}],它应该可以工作,但它需要提前知道所有子类。
猜你喜欢
  • 2017-08-28
  • 2020-05-12
  • 2021-08-13
  • 2016-08-01
  • 2018-08-28
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多