在我的nativescript-angular项目中,我有一个这样的组件:
<StackLayout>
<Label *ngIf="ngContentIsEmpty"> ng-content is empty </Label>
<ng-content></ng-content>
</StackLayout>现在,我想显示一些内容,如果这个组件的<ng-content>是empty.Is,有一个简单的方法吗?
我已经尝试过了,但不能在nativescript中工作:
<StackLayout #wrapper>
<Label *ngIf="wrapper.childNodes.length == 0"> ng-content is empty </Label>
<ng-content></ng-content>
</StackLayout>发布于 2019-08-12 14:16:40
尝试执行此操作以检查ng-content为空。
添加到Html文件:-
<StackLayout *ngIf="ngContentIsEmpty">Content</StackLayout>
<StackLayout #wrapper>
<ng-content></ng-content>
</StackLayout>在TypeScript文件中:
export class MyComponent implements OnInit, AfterContentInit {
@ContentChild('wrapper') wrapper:ElementRef;
public ngContentIsEmpty = false;
ngAfterContentInit() {
this.ngContentIsEmpty= this.wrapper.childNodes.length > 0;
}
}https://stackoverflow.com/questions/57449830
复制相似问题