首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nativescript-angular中如何检查<ng-content>是否为空?

在nativescript-angular中如何检查<ng-content>是否为空?
EN

Stack Overflow用户
提问于 2019-08-11 20:09:02
回答 1查看 162关注 0票数 0

在我的nativescript-angular项目中,我有一个这样的组件:

代码语言:javascript
复制
<StackLayout>
    <Label *ngIf="ngContentIsEmpty"> ng-content is empty </Label>
    <ng-content></ng-content>
</StackLayout>

现在,我想显示一些内容,如果这个组件的<ng-content>是empty.Is,有一个简单的方法吗?

我已经尝试过了,但不能在nativescript中工作:

代码语言:javascript
复制
<StackLayout #wrapper>
    <Label *ngIf="wrapper.childNodes.length == 0"> ng-content is empty </Label>
    <ng-content></ng-content>
</StackLayout>
EN

回答 1

Stack Overflow用户

发布于 2019-08-12 14:16:40

尝试执行此操作以检查ng-content为空。

添加到Html文件:-

代码语言:javascript
复制
<StackLayout *ngIf="ngContentIsEmpty">Content</StackLayout>
<StackLayout #wrapper>
    <ng-content></ng-content>
</StackLayout>

在TypeScript文件中:

代码语言:javascript
复制
export class MyComponent implements OnInit, AfterContentInit {

  @ContentChild('wrapper') wrapper:ElementRef;
  public ngContentIsEmpty = false;

  ngAfterContentInit() {
   this.ngContentIsEmpty= this.wrapper.childNodes.length > 0;
  }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57449830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档