在angular2中,我如何检测是否有任何ng-content?

In angular2, how can I detect is there any ng-content?

plnkr 演示 here

@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 有可能吗?

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`
}

另见

如果你想对 *ngIfelse 使用条件语句,可以

@Component({
  selector: 'my-demo',
  template: `<div *ngIf='content; else noContent'>
This is <ng-content select=".content"></ng-content>
</div>

<ng-template #noContent>No content</ng-template>`
})
export class DemoComponent { name = 'Angular'; content = true}

Demo

Günter Zöchbauer 的解决方案是可以接受的,不影响使用,让组件检测到这一点,我还找到了一种更简单的方法,无需任何 json 通过使用 :empty

<!-- must no content: https://css-tricks.com/almanac/selectors/e/empty/ -->
<!--@formatter:off-->
<div class="title"><ng-content select=".nav-title"></ng-content></div>
<!--@formatter:on-->

.title:empty {
  display: none;
}

适用于任何 html+css。