2
我有一个场景,我需要根据布尔标志在屏幕上呈现div。该标志的值是从服务接收的。检测角度变化值2
<div *ngIf="isEnabled()" class="col-sm-3 col-md-2 sidebar">
<menu-bar></menu-bar>
</div>
当服务返回一个布尔值时,isEnabled函数返回null。有什么办法可以在订阅服务成功后呈现这个div?
下面是我使用的组件:
export class MainAppComponent implements OnInit{
static enabled : boolean = true;
license : string;
errorMessage : string;
fatalError : boolean;
mode = 'Observable';
constructor(private licenseService : LicenceService){}
ngOnInit(): void {
this.licenseService.getLicenseInfo()
.subscribe(info => {
if(info) {
SharedService.isLicesePresent = true;
} else {
SharedService.isLicesePresent = false;
}
},
error => {
this.fatalError = true;
this.errorMessage = <any>error;
swal({
title: ' Error',
type: 'warning',
text: 'Network Error. Please contact customer support',
})
});
}
isEnabled() : boolean {
return SharedService.isLicesePresent;
}
}
后写在'的IsEnabled代码( )'function –
什么是'SharedService'。它应该有一个'this.'。 –
它是一种带有静态变量isLicesePresent的实用类。 –