在angular2中,如何检测到任何ng含量? [英] In angular2, how can I detect is there any ng-content?

查看:90
本文介绍了在angular2中,如何检测到任何ng含量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

plnkr演示此处

@Component({
  selector: 'my-demo',
  template: `This is <ng-content select=".content"></ng-content>`
})
export class DemoComponent { name = 'Angular'; }

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>
    <my-demo><div class="content">In Content</div></my-demo>
  `
})
export class AppComponent { name = 'Angular'; }

我想有条件地ng-content,例如

<ng-template *ngIf='hasContent(".content"); else noContent'>
This is <ng-content select=".content"></ng-content>
</ng-template>
<ng-template #noContent>No content</ng-template>

angular2是否有可能?

Is there possible in angular2 ?

推荐答案

template: `This is <span #wrapper>
  <ng-content select=".content"></ng-content>
  </span>`

@ViewChild('wrapper') wrapper:ElementRef;

ngAfterContentInit() {
    console.log(this.wrapper.innerHTML); // or `wrapper.text`
}

另请参见

  • Get component child as string
  • Access transcluded content

这篇关于在angular2中,如何检测到任何ng含量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆