angular 2 access ng-content within component

后端 未结 4 1988
半阙折子戏
半阙折子戏 2020-12-01 23:26

How can I access the \"content\" of a component from within the component class itself?

I would like to do something like this:

<         


        
4条回答
  •  一个人的身影
    2020-12-01 23:50

    https://angular.io/api/core/ContentChildren

    class SomeDir implements AfterContentInit {
    
      @ContentChildren(ChildDirective) contentChildren : QueryList;
    
      ngAfterContentInit() {
        // contentChildren is set
      }
    }
    

    Note that if you do console.log(contentChildren), it will only work on ngAfterContentInit or a later event.

提交回复
热议问题