kittencup / angular2-ama-cn

angular2 随便问
692 stars 101 forks source link

如何使ng-content无内容时隐藏 #254

Closed scgy5555 closed 7 years ago

scgy5555 commented 7 years ago
    <div v-if="$slots.default">
      <slot></slot>
    </div>

这是一段标准的vue代码,想通过angular2实现相同的效果。 其功能是判断嵌入子元素是否存在。

翻了很多资料ng-content本身似乎没有这种变量。 使用ContentChild也只能用于#标记的ElementRef。

有没有办法在不指定嵌入子元素类型(Class)的时候判断嵌入子元素是否存在。 学艺不精,或许思路就有问题,还请大大指点。

hstarorg commented 7 years ago
<ng-content select="[slot=modal-footer]"></ng-content>
<ng-template [ngIf]="!hasCustomFooter">
  xxxxxxx
</ng-template>
this.hasCustomFooter = !!this.modalFooter.nativeElement.querySelector('[slot=modal-footer]');