小编Ric*_*ard的帖子

我如何访问ng-content?

我想访问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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我将什么作为内容儿童的类型?

它们可以是任何东西.我尝试过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>
Run Code Online (Sandbox Code Playgroud)

我希望看到"在内容init:4之后".

angular

6
推荐指数
1
解决办法
5026
查看次数

标签 统计

angular ×1